
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
  <head>
    <meta http-equiv="X-UA-Compatible" content="IE=Edge" />
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>django.db.backends.base.schema &#8212; Django 2.2.12.dev20200304094918 documentation</title>
    <link rel="stylesheet" href="../../../../../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../../../../../_static/pygments.css" type="text/css" />
    <script type="text/javascript" id="documentation_options" data-url_root="../../../../../" src="../../../../../_static/documentation_options.js"></script>
    <script type="text/javascript" src="../../../../../_static/jquery.js"></script>
    <script type="text/javascript" src="../../../../../_static/underscore.js"></script>
    <script type="text/javascript" src="../../../../../_static/doctools.js"></script>
    <script type="text/javascript" src="../../../../../_static/language_data.js"></script>
    <link rel="index" title="Index" href="../../../../../genindex.html" />
    <link rel="search" title="Search" href="../../../../../search.html" />



 
<script type="text/javascript" src="../../../../../templatebuiltins.js"></script>
<script type="text/javascript">
(function($) {
    if (!django_template_builtins) {
       // templatebuiltins.js missing, do nothing.
       return;
    }
    $(document).ready(function() {
        // Hyperlink Django template tags and filters
        var base = "../../../../../ref/templates/builtins.html";
        if (base == "#") {
            // Special case for builtins.html itself
            base = "";
        }
        // Tags are keywords, class '.k'
        $("div.highlight\\-html\\+django span.k").each(function(i, elem) {
             var tagname = $(elem).text();
             if ($.inArray(tagname, django_template_builtins.ttags) != -1) {
                 var fragment = tagname.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + tagname + "</a>");
             }
        });
        // Filters are functions, class '.nf'
        $("div.highlight\\-html\\+django span.nf").each(function(i, elem) {
             var filtername = $(elem).text();
             if ($.inArray(filtername, django_template_builtins.tfilters) != -1) {
                 var fragment = filtername.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + filtername + "</a>");
             }
        });
    });
})(jQuery);</script>

  </head><body>

    <div class="document">
  <div id="custom-doc" class="yui-t6">
    <div id="hd">
      <h1><a href="../../../../../index.html">Django 2.2.12.dev20200304094918 documentation</a></h1>
      <div id="global-nav">
        <a title="Home page" href="../../../../../index.html">Home</a>  |
        <a title="Table of contents" href="../../../../../contents.html">Table of contents</a>  |
        <a title="Global index" href="../../../../../genindex.html">Index</a>  |
        <a title="Module index" href="../../../../../py-modindex.html">Modules</a>
      </div>
      <div class="nav">
    <a href="../../../../index.html" title="Module code" accesskey="U">up</a></div>
    </div>

    <div id="bd">
      <div id="yui-main">
        <div class="yui-b">
          <div class="yui-g" id="_modules-django-db-backends-base-schema">
            
  <h1>Source code for django.db.backends.base.schema</h1><div class="highlight"><pre>
<span></span><span class="kn">import</span> <span class="nn">logging</span>
<span class="kn">from</span> <span class="nn">datetime</span> <span class="k">import</span> <span class="n">datetime</span>

<span class="kn">from</span> <span class="nn">django.db.backends.ddl_references</span> <span class="k">import</span> <span class="p">(</span>
    <span class="n">Columns</span><span class="p">,</span> <span class="n">ForeignKeyName</span><span class="p">,</span> <span class="n">IndexName</span><span class="p">,</span> <span class="n">Statement</span><span class="p">,</span> <span class="n">Table</span><span class="p">,</span>
<span class="p">)</span>
<span class="kn">from</span> <span class="nn">django.db.backends.utils</span> <span class="k">import</span> <span class="n">names_digest</span><span class="p">,</span> <span class="n">split_identifier</span>
<span class="kn">from</span> <span class="nn">django.db.models</span> <span class="k">import</span> <span class="n">Index</span>
<span class="kn">from</span> <span class="nn">django.db.transaction</span> <span class="k">import</span> <span class="n">TransactionManagementError</span><span class="p">,</span> <span class="n">atomic</span>
<span class="kn">from</span> <span class="nn">django.utils</span> <span class="k">import</span> <span class="n">timezone</span>

<span class="n">logger</span> <span class="o">=</span> <span class="n">logging</span><span class="o">.</span><span class="n">getLogger</span><span class="p">(</span><span class="s1">&#39;django.db.backends.schema&#39;</span><span class="p">)</span>


<span class="k">def</span> <span class="nf">_is_relevant_relation</span><span class="p">(</span><span class="n">relation</span><span class="p">,</span> <span class="n">altered_field</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    When altering the given field, must constraints on its model from the given</span>
<span class="sd">    relation be temporarily dropped?</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">field</span> <span class="o">=</span> <span class="n">relation</span><span class="o">.</span><span class="n">field</span>
    <span class="k">if</span> <span class="n">field</span><span class="o">.</span><span class="n">many_to_many</span><span class="p">:</span>
        <span class="c1"># M2M reverse field</span>
        <span class="k">return</span> <span class="kc">False</span>
    <span class="k">if</span> <span class="n">altered_field</span><span class="o">.</span><span class="n">primary_key</span> <span class="ow">and</span> <span class="n">field</span><span class="o">.</span><span class="n">to_fields</span> <span class="o">==</span> <span class="p">[</span><span class="kc">None</span><span class="p">]:</span>
        <span class="c1"># Foreign key constraint on the primary key, which is being altered.</span>
        <span class="k">return</span> <span class="kc">True</span>
    <span class="c1"># Is the constraint targeting the field being altered?</span>
    <span class="k">return</span> <span class="n">altered_field</span><span class="o">.</span><span class="n">name</span> <span class="ow">in</span> <span class="n">field</span><span class="o">.</span><span class="n">to_fields</span>


<span class="k">def</span> <span class="nf">_related_non_m2m_objects</span><span class="p">(</span><span class="n">old_field</span><span class="p">,</span> <span class="n">new_field</span><span class="p">):</span>
    <span class="c1"># Filter out m2m objects from reverse relations.</span>
    <span class="c1"># Return (old_relation, new_relation) tuples.</span>
    <span class="k">return</span> <span class="nb">zip</span><span class="p">(</span>
        <span class="p">(</span><span class="n">obj</span> <span class="k">for</span> <span class="n">obj</span> <span class="ow">in</span> <span class="n">old_field</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">related_objects</span> <span class="k">if</span> <span class="n">_is_relevant_relation</span><span class="p">(</span><span class="n">obj</span><span class="p">,</span> <span class="n">old_field</span><span class="p">)),</span>
        <span class="p">(</span><span class="n">obj</span> <span class="k">for</span> <span class="n">obj</span> <span class="ow">in</span> <span class="n">new_field</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">related_objects</span> <span class="k">if</span> <span class="n">_is_relevant_relation</span><span class="p">(</span><span class="n">obj</span><span class="p">,</span> <span class="n">new_field</span><span class="p">))</span>
    <span class="p">)</span>


<div class="viewcode-block" id="BaseDatabaseSchemaEditor"><a class="viewcode-back" href="../../../../../ref/schema-editor.html#django.db.backends.base.schema.BaseDatabaseSchemaEditor">[docs]</a><span class="k">class</span> <span class="nc">BaseDatabaseSchemaEditor</span><span class="p">:</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    This class and its subclasses are responsible for emitting schema-changing</span>
<span class="sd">    statements to the databases - model creation/removal/alteration, field</span>
<span class="sd">    renaming, index fiddling, and so on.</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="c1"># Overrideable SQL templates</span>
    <span class="n">sql_create_table</span> <span class="o">=</span> <span class="s2">&quot;CREATE TABLE </span><span class="si">%(table)s</span><span class="s2"> (</span><span class="si">%(definition)s</span><span class="s2">)&quot;</span>
    <span class="n">sql_rename_table</span> <span class="o">=</span> <span class="s2">&quot;ALTER TABLE </span><span class="si">%(old_table)s</span><span class="s2"> RENAME TO </span><span class="si">%(new_table)s</span><span class="s2">&quot;</span>
    <span class="n">sql_retablespace_table</span> <span class="o">=</span> <span class="s2">&quot;ALTER TABLE </span><span class="si">%(table)s</span><span class="s2"> SET TABLESPACE </span><span class="si">%(new_tablespace)s</span><span class="s2">&quot;</span>
    <span class="n">sql_delete_table</span> <span class="o">=</span> <span class="s2">&quot;DROP TABLE </span><span class="si">%(table)s</span><span class="s2"> CASCADE&quot;</span>

    <span class="n">sql_create_column</span> <span class="o">=</span> <span class="s2">&quot;ALTER TABLE </span><span class="si">%(table)s</span><span class="s2"> ADD COLUMN </span><span class="si">%(column)s</span><span class="s2"> </span><span class="si">%(definition)s</span><span class="s2">&quot;</span>
    <span class="n">sql_alter_column</span> <span class="o">=</span> <span class="s2">&quot;ALTER TABLE </span><span class="si">%(table)s</span><span class="s2"> </span><span class="si">%(changes)s</span><span class="s2">&quot;</span>
    <span class="n">sql_alter_column_type</span> <span class="o">=</span> <span class="s2">&quot;ALTER COLUMN </span><span class="si">%(column)s</span><span class="s2"> TYPE </span><span class="si">%(type)s</span><span class="s2">&quot;</span>
    <span class="n">sql_alter_column_null</span> <span class="o">=</span> <span class="s2">&quot;ALTER COLUMN </span><span class="si">%(column)s</span><span class="s2"> DROP NOT NULL&quot;</span>
    <span class="n">sql_alter_column_not_null</span> <span class="o">=</span> <span class="s2">&quot;ALTER COLUMN </span><span class="si">%(column)s</span><span class="s2"> SET NOT NULL&quot;</span>
    <span class="n">sql_alter_column_default</span> <span class="o">=</span> <span class="s2">&quot;ALTER COLUMN </span><span class="si">%(column)s</span><span class="s2"> SET DEFAULT </span><span class="si">%(default)s</span><span class="s2">&quot;</span>
    <span class="n">sql_alter_column_no_default</span> <span class="o">=</span> <span class="s2">&quot;ALTER COLUMN </span><span class="si">%(column)s</span><span class="s2"> DROP DEFAULT&quot;</span>
    <span class="n">sql_delete_column</span> <span class="o">=</span> <span class="s2">&quot;ALTER TABLE </span><span class="si">%(table)s</span><span class="s2"> DROP COLUMN </span><span class="si">%(column)s</span><span class="s2"> CASCADE&quot;</span>
    <span class="n">sql_rename_column</span> <span class="o">=</span> <span class="s2">&quot;ALTER TABLE </span><span class="si">%(table)s</span><span class="s2"> RENAME COLUMN </span><span class="si">%(old_column)s</span><span class="s2"> TO </span><span class="si">%(new_column)s</span><span class="s2">&quot;</span>
    <span class="n">sql_update_with_default</span> <span class="o">=</span> <span class="s2">&quot;UPDATE </span><span class="si">%(table)s</span><span class="s2"> SET </span><span class="si">%(column)s</span><span class="s2"> = </span><span class="si">%(default)s</span><span class="s2"> WHERE </span><span class="si">%(column)s</span><span class="s2"> IS NULL&quot;</span>

    <span class="n">sql_unique_constraint</span> <span class="o">=</span> <span class="s2">&quot;UNIQUE (</span><span class="si">%(columns)s</span><span class="s2">)&quot;</span>
    <span class="n">sql_check_constraint</span> <span class="o">=</span> <span class="s2">&quot;CHECK (</span><span class="si">%(check)s</span><span class="s2">)&quot;</span>
    <span class="n">sql_delete_constraint</span> <span class="o">=</span> <span class="s2">&quot;ALTER TABLE </span><span class="si">%(table)s</span><span class="s2"> DROP CONSTRAINT </span><span class="si">%(name)s</span><span class="s2">&quot;</span>
    <span class="n">sql_constraint</span> <span class="o">=</span> <span class="s2">&quot;CONSTRAINT </span><span class="si">%(name)s</span><span class="s2"> </span><span class="si">%(constraint)s</span><span class="s2">&quot;</span>

    <span class="n">sql_create_check</span> <span class="o">=</span> <span class="s2">&quot;ALTER TABLE </span><span class="si">%(table)s</span><span class="s2"> ADD CONSTRAINT </span><span class="si">%(name)s</span><span class="s2"> CHECK (</span><span class="si">%(check)s</span><span class="s2">)&quot;</span>
    <span class="n">sql_delete_check</span> <span class="o">=</span> <span class="n">sql_delete_constraint</span>

    <span class="n">sql_create_unique</span> <span class="o">=</span> <span class="s2">&quot;ALTER TABLE </span><span class="si">%(table)s</span><span class="s2"> ADD CONSTRAINT </span><span class="si">%(name)s</span><span class="s2"> UNIQUE (</span><span class="si">%(columns)s</span><span class="s2">)&quot;</span>
    <span class="n">sql_delete_unique</span> <span class="o">=</span> <span class="n">sql_delete_constraint</span>

    <span class="n">sql_create_fk</span> <span class="o">=</span> <span class="p">(</span>
        <span class="s2">&quot;ALTER TABLE </span><span class="si">%(table)s</span><span class="s2"> ADD CONSTRAINT </span><span class="si">%(name)s</span><span class="s2"> FOREIGN KEY (</span><span class="si">%(column)s</span><span class="s2">) &quot;</span>
        <span class="s2">&quot;REFERENCES </span><span class="si">%(to_table)s</span><span class="s2"> (</span><span class="si">%(to_column)s</span><span class="s2">)</span><span class="si">%(deferrable)s</span><span class="s2">&quot;</span>
    <span class="p">)</span>
    <span class="n">sql_create_inline_fk</span> <span class="o">=</span> <span class="kc">None</span>
    <span class="n">sql_delete_fk</span> <span class="o">=</span> <span class="n">sql_delete_constraint</span>

    <span class="n">sql_create_index</span> <span class="o">=</span> <span class="s2">&quot;CREATE INDEX </span><span class="si">%(name)s</span><span class="s2"> ON </span><span class="si">%(table)s</span><span class="s2"> (</span><span class="si">%(columns)s</span><span class="s2">)</span><span class="si">%(extra)s%(condition)s</span><span class="s2">&quot;</span>
    <span class="n">sql_create_unique_index</span> <span class="o">=</span> <span class="s2">&quot;CREATE UNIQUE INDEX </span><span class="si">%(name)s</span><span class="s2"> ON </span><span class="si">%(table)s</span><span class="s2"> (</span><span class="si">%(columns)s</span><span class="s2">)</span><span class="si">%(condition)s</span><span class="s2">&quot;</span>
    <span class="n">sql_delete_index</span> <span class="o">=</span> <span class="s2">&quot;DROP INDEX </span><span class="si">%(name)s</span><span class="s2">&quot;</span>

    <span class="n">sql_create_pk</span> <span class="o">=</span> <span class="s2">&quot;ALTER TABLE </span><span class="si">%(table)s</span><span class="s2"> ADD CONSTRAINT </span><span class="si">%(name)s</span><span class="s2"> PRIMARY KEY (</span><span class="si">%(columns)s</span><span class="s2">)&quot;</span>
    <span class="n">sql_delete_pk</span> <span class="o">=</span> <span class="n">sql_delete_constraint</span>

    <span class="n">sql_delete_procedure</span> <span class="o">=</span> <span class="s1">&#39;DROP PROCEDURE </span><span class="si">%(procedure)s</span><span class="s1">&#39;</span>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">connection</span><span class="p">,</span> <span class="n">collect_sql</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="n">atomic</span><span class="o">=</span><span class="kc">True</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">connection</span> <span class="o">=</span> <span class="n">connection</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">collect_sql</span> <span class="o">=</span> <span class="n">collect_sql</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">collect_sql</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">collected_sql</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">atomic_migration</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">connection</span><span class="o">.</span><span class="n">features</span><span class="o">.</span><span class="n">can_rollback_ddl</span> <span class="ow">and</span> <span class="n">atomic</span>

    <span class="c1"># State-managing methods</span>

    <span class="k">def</span> <span class="nf">__enter__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">deferred_sql</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">atomic_migration</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">atomic</span> <span class="o">=</span> <span class="n">atomic</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">connection</span><span class="o">.</span><span class="n">alias</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">atomic</span><span class="o">.</span><span class="fm">__enter__</span><span class="p">()</span>
        <span class="k">return</span> <span class="bp">self</span>

    <span class="k">def</span> <span class="nf">__exit__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">exc_type</span><span class="p">,</span> <span class="n">exc_value</span><span class="p">,</span> <span class="n">traceback</span><span class="p">):</span>
        <span class="k">if</span> <span class="n">exc_type</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="k">for</span> <span class="n">sql</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">deferred_sql</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="n">sql</span><span class="p">)</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">atomic_migration</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">atomic</span><span class="o">.</span><span class="fm">__exit__</span><span class="p">(</span><span class="n">exc_type</span><span class="p">,</span> <span class="n">exc_value</span><span class="p">,</span> <span class="n">traceback</span><span class="p">)</span>

    <span class="c1"># Core utility functions</span>

<div class="viewcode-block" id="BaseDatabaseSchemaEditor.execute"><a class="viewcode-back" href="../../../../../ref/schema-editor.html#django.db.backends.base.schema.BaseDatabaseSchemaEditor.execute">[docs]</a>    <span class="k">def</span> <span class="nf">execute</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">sql</span><span class="p">,</span> <span class="n">params</span><span class="o">=</span><span class="p">()):</span>
        <span class="sd">&quot;&quot;&quot;Execute the given SQL statement, with optional parameters.&quot;&quot;&quot;</span>
        <span class="c1"># Don&#39;t perform the transactional DDL check if SQL is being collected</span>
        <span class="c1"># as it&#39;s not going to be executed anyway.</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">collect_sql</span> <span class="ow">and</span> <span class="bp">self</span><span class="o">.</span><span class="n">connection</span><span class="o">.</span><span class="n">in_atomic_block</span> <span class="ow">and</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">connection</span><span class="o">.</span><span class="n">features</span><span class="o">.</span><span class="n">can_rollback_ddl</span><span class="p">:</span>
            <span class="k">raise</span> <span class="n">TransactionManagementError</span><span class="p">(</span>
                <span class="s2">&quot;Executing DDL statements while in a transaction on databases &quot;</span>
                <span class="s2">&quot;that can&#39;t perform a rollback is prohibited.&quot;</span>
            <span class="p">)</span>
        <span class="c1"># Account for non-string statement objects.</span>
        <span class="n">sql</span> <span class="o">=</span> <span class="nb">str</span><span class="p">(</span><span class="n">sql</span><span class="p">)</span>
        <span class="c1"># Log the command we&#39;re running, then run it</span>
        <span class="n">logger</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s2">&quot;</span><span class="si">%s</span><span class="s2">; (params </span><span class="si">%r</span><span class="s2">)&quot;</span><span class="p">,</span> <span class="n">sql</span><span class="p">,</span> <span class="n">params</span><span class="p">,</span> <span class="n">extra</span><span class="o">=</span><span class="p">{</span><span class="s1">&#39;params&#39;</span><span class="p">:</span> <span class="n">params</span><span class="p">,</span> <span class="s1">&#39;sql&#39;</span><span class="p">:</span> <span class="n">sql</span><span class="p">})</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">collect_sql</span><span class="p">:</span>
            <span class="n">ending</span> <span class="o">=</span> <span class="s2">&quot;&quot;</span> <span class="k">if</span> <span class="n">sql</span><span class="o">.</span><span class="n">endswith</span><span class="p">(</span><span class="s2">&quot;;&quot;</span><span class="p">)</span> <span class="k">else</span> <span class="s2">&quot;;&quot;</span>
            <span class="k">if</span> <span class="n">params</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">collected_sql</span><span class="o">.</span><span class="n">append</span><span class="p">((</span><span class="n">sql</span> <span class="o">%</span> <span class="nb">tuple</span><span class="p">(</span><span class="nb">map</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">quote_value</span><span class="p">,</span> <span class="n">params</span><span class="p">)))</span> <span class="o">+</span> <span class="n">ending</span><span class="p">)</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">collected_sql</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">sql</span> <span class="o">+</span> <span class="n">ending</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">with</span> <span class="bp">self</span><span class="o">.</span><span class="n">connection</span><span class="o">.</span><span class="n">cursor</span><span class="p">()</span> <span class="k">as</span> <span class="n">cursor</span><span class="p">:</span>
                <span class="n">cursor</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="n">sql</span><span class="p">,</span> <span class="n">params</span><span class="p">)</span></div>

    <span class="k">def</span> <span class="nf">quote_name</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">name</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">connection</span><span class="o">.</span><span class="n">ops</span><span class="o">.</span><span class="n">quote_name</span><span class="p">(</span><span class="n">name</span><span class="p">)</span>

    <span class="c1"># Field &lt;-&gt; database mapping functions</span>

    <span class="k">def</span> <span class="nf">column_sql</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">model</span><span class="p">,</span> <span class="n">field</span><span class="p">,</span> <span class="n">include_default</span><span class="o">=</span><span class="kc">False</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Take a field and return its column definition.</span>
<span class="sd">        The field must already have had set_attributes_from_name() called.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="c1"># Get the column&#39;s type and use that as the basis of the SQL</span>
        <span class="n">db_params</span> <span class="o">=</span> <span class="n">field</span><span class="o">.</span><span class="n">db_parameters</span><span class="p">(</span><span class="n">connection</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">connection</span><span class="p">)</span>
        <span class="n">sql</span> <span class="o">=</span> <span class="n">db_params</span><span class="p">[</span><span class="s1">&#39;type&#39;</span><span class="p">]</span>
        <span class="n">params</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="c1"># Check for fields that aren&#39;t actually columns (e.g. M2M)</span>
        <span class="k">if</span> <span class="n">sql</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="k">return</span> <span class="kc">None</span><span class="p">,</span> <span class="kc">None</span>
        <span class="c1"># Work out nullability</span>
        <span class="n">null</span> <span class="o">=</span> <span class="n">field</span><span class="o">.</span><span class="n">null</span>
        <span class="c1"># If we were told to include a default value, do so</span>
        <span class="n">include_default</span> <span class="o">=</span> <span class="n">include_default</span> <span class="ow">and</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">skip_default</span><span class="p">(</span><span class="n">field</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">include_default</span><span class="p">:</span>
            <span class="n">default_value</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">effective_default</span><span class="p">(</span><span class="n">field</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">default_value</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
                <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">connection</span><span class="o">.</span><span class="n">features</span><span class="o">.</span><span class="n">requires_literal_defaults</span><span class="p">:</span>
                    <span class="c1"># Some databases can&#39;t take defaults as a parameter (oracle)</span>
                    <span class="c1"># If this is the case, the individual schema backend should</span>
                    <span class="c1"># implement prepare_default</span>
                    <span class="n">sql</span> <span class="o">+=</span> <span class="s2">&quot; DEFAULT </span><span class="si">%s</span><span class="s2">&quot;</span> <span class="o">%</span> <span class="bp">self</span><span class="o">.</span><span class="n">prepare_default</span><span class="p">(</span><span class="n">default_value</span><span class="p">)</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="n">sql</span> <span class="o">+=</span> <span class="s2">&quot; DEFAULT </span><span class="si">%s</span><span class="s2">&quot;</span>
                    <span class="n">params</span> <span class="o">+=</span> <span class="p">[</span><span class="n">default_value</span><span class="p">]</span>
        <span class="c1"># Oracle treats the empty string (&#39;&#39;) as null, so coerce the null</span>
        <span class="c1"># option whenever &#39;&#39; is a possible value.</span>
        <span class="k">if</span> <span class="p">(</span><span class="n">field</span><span class="o">.</span><span class="n">empty_strings_allowed</span> <span class="ow">and</span> <span class="ow">not</span> <span class="n">field</span><span class="o">.</span><span class="n">primary_key</span> <span class="ow">and</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">connection</span><span class="o">.</span><span class="n">features</span><span class="o">.</span><span class="n">interprets_empty_strings_as_nulls</span><span class="p">):</span>
            <span class="n">null</span> <span class="o">=</span> <span class="kc">True</span>
        <span class="k">if</span> <span class="n">null</span> <span class="ow">and</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">connection</span><span class="o">.</span><span class="n">features</span><span class="o">.</span><span class="n">implied_column_null</span><span class="p">:</span>
            <span class="n">sql</span> <span class="o">+=</span> <span class="s2">&quot; NULL&quot;</span>
        <span class="k">elif</span> <span class="ow">not</span> <span class="n">null</span><span class="p">:</span>
            <span class="n">sql</span> <span class="o">+=</span> <span class="s2">&quot; NOT NULL&quot;</span>
        <span class="c1"># Primary key/unique outputs</span>
        <span class="k">if</span> <span class="n">field</span><span class="o">.</span><span class="n">primary_key</span><span class="p">:</span>
            <span class="n">sql</span> <span class="o">+=</span> <span class="s2">&quot; PRIMARY KEY&quot;</span>
        <span class="k">elif</span> <span class="n">field</span><span class="o">.</span><span class="n">unique</span><span class="p">:</span>
            <span class="n">sql</span> <span class="o">+=</span> <span class="s2">&quot; UNIQUE&quot;</span>
        <span class="c1"># Optionally add the tablespace if it&#39;s an implicitly indexed column</span>
        <span class="n">tablespace</span> <span class="o">=</span> <span class="n">field</span><span class="o">.</span><span class="n">db_tablespace</span> <span class="ow">or</span> <span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">db_tablespace</span>
        <span class="k">if</span> <span class="n">tablespace</span> <span class="ow">and</span> <span class="bp">self</span><span class="o">.</span><span class="n">connection</span><span class="o">.</span><span class="n">features</span><span class="o">.</span><span class="n">supports_tablespaces</span> <span class="ow">and</span> <span class="n">field</span><span class="o">.</span><span class="n">unique</span><span class="p">:</span>
            <span class="n">sql</span> <span class="o">+=</span> <span class="s2">&quot; </span><span class="si">%s</span><span class="s2">&quot;</span> <span class="o">%</span> <span class="bp">self</span><span class="o">.</span><span class="n">connection</span><span class="o">.</span><span class="n">ops</span><span class="o">.</span><span class="n">tablespace_sql</span><span class="p">(</span><span class="n">tablespace</span><span class="p">,</span> <span class="n">inline</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
        <span class="c1"># Return the sql</span>
        <span class="k">return</span> <span class="n">sql</span><span class="p">,</span> <span class="n">params</span>

    <span class="k">def</span> <span class="nf">skip_default</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">field</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Some backends don&#39;t accept default values for certain columns types</span>
<span class="sd">        (i.e. MySQL longtext and longblob).</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="kc">False</span>

    <span class="k">def</span> <span class="nf">prepare_default</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">value</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Only used for backends which have requires_literal_defaults feature</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">raise</span> <span class="ne">NotImplementedError</span><span class="p">(</span>
            <span class="s1">&#39;subclasses of BaseDatabaseSchemaEditor for backends which have &#39;</span>
            <span class="s1">&#39;requires_literal_defaults must provide a prepare_default() method&#39;</span>
        <span class="p">)</span>

    <span class="nd">@staticmethod</span>
    <span class="k">def</span> <span class="nf">_effective_default</span><span class="p">(</span><span class="n">field</span><span class="p">):</span>
        <span class="c1"># This method allows testing its logic without a connection.</span>
        <span class="k">if</span> <span class="n">field</span><span class="o">.</span><span class="n">has_default</span><span class="p">():</span>
            <span class="n">default</span> <span class="o">=</span> <span class="n">field</span><span class="o">.</span><span class="n">get_default</span><span class="p">()</span>
        <span class="k">elif</span> <span class="ow">not</span> <span class="n">field</span><span class="o">.</span><span class="n">null</span> <span class="ow">and</span> <span class="n">field</span><span class="o">.</span><span class="n">blank</span> <span class="ow">and</span> <span class="n">field</span><span class="o">.</span><span class="n">empty_strings_allowed</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">field</span><span class="o">.</span><span class="n">get_internal_type</span><span class="p">()</span> <span class="o">==</span> <span class="s2">&quot;BinaryField&quot;</span><span class="p">:</span>
                <span class="n">default</span> <span class="o">=</span> <span class="nb">bytes</span><span class="p">()</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">default</span> <span class="o">=</span> <span class="nb">str</span><span class="p">()</span>
        <span class="k">elif</span> <span class="nb">getattr</span><span class="p">(</span><span class="n">field</span><span class="p">,</span> <span class="s1">&#39;auto_now&#39;</span><span class="p">,</span> <span class="kc">False</span><span class="p">)</span> <span class="ow">or</span> <span class="nb">getattr</span><span class="p">(</span><span class="n">field</span><span class="p">,</span> <span class="s1">&#39;auto_now_add&#39;</span><span class="p">,</span> <span class="kc">False</span><span class="p">):</span>
            <span class="n">default</span> <span class="o">=</span> <span class="n">datetime</span><span class="o">.</span><span class="n">now</span><span class="p">()</span>
            <span class="n">internal_type</span> <span class="o">=</span> <span class="n">field</span><span class="o">.</span><span class="n">get_internal_type</span><span class="p">()</span>
            <span class="k">if</span> <span class="n">internal_type</span> <span class="o">==</span> <span class="s1">&#39;DateField&#39;</span><span class="p">:</span>
                <span class="n">default</span> <span class="o">=</span> <span class="n">default</span><span class="o">.</span><span class="n">date</span><span class="p">()</span>
            <span class="k">elif</span> <span class="n">internal_type</span> <span class="o">==</span> <span class="s1">&#39;TimeField&#39;</span><span class="p">:</span>
                <span class="n">default</span> <span class="o">=</span> <span class="n">default</span><span class="o">.</span><span class="n">time</span><span class="p">()</span>
            <span class="k">elif</span> <span class="n">internal_type</span> <span class="o">==</span> <span class="s1">&#39;DateTimeField&#39;</span><span class="p">:</span>
                <span class="n">default</span> <span class="o">=</span> <span class="n">timezone</span><span class="o">.</span><span class="n">now</span><span class="p">()</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">default</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="k">return</span> <span class="n">default</span>

    <span class="k">def</span> <span class="nf">effective_default</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">field</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Return a field&#39;s effective database default value.&quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="n">field</span><span class="o">.</span><span class="n">get_db_prep_save</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_effective_default</span><span class="p">(</span><span class="n">field</span><span class="p">),</span> <span class="bp">self</span><span class="o">.</span><span class="n">connection</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">quote_value</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">value</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Return a quoted version of the value so it&#39;s safe to use in an SQL</span>
<span class="sd">        string. This is not safe against injection from user code; it is</span>
<span class="sd">        intended only for use in making SQL scripts or preparing default values</span>
<span class="sd">        for particularly tricky backends (defaults are not user-defined, though,</span>
<span class="sd">        so this is safe).</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">raise</span> <span class="ne">NotImplementedError</span><span class="p">()</span>

    <span class="c1"># Actions</span>

<div class="viewcode-block" id="BaseDatabaseSchemaEditor.create_model"><a class="viewcode-back" href="../../../../../ref/schema-editor.html#django.db.backends.base.schema.BaseDatabaseSchemaEditor.create_model">[docs]</a>    <span class="k">def</span> <span class="nf">create_model</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">model</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Create a table and any accompanying indexes or unique constraints for</span>
<span class="sd">        the given `model`.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="c1"># Create column SQL, add FK deferreds if needed</span>
        <span class="n">column_sqls</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="n">params</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="k">for</span> <span class="n">field</span> <span class="ow">in</span> <span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">local_fields</span><span class="p">:</span>
            <span class="c1"># SQL</span>
            <span class="n">definition</span><span class="p">,</span> <span class="n">extra_params</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">column_sql</span><span class="p">(</span><span class="n">model</span><span class="p">,</span> <span class="n">field</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">definition</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
                <span class="k">continue</span>
            <span class="c1"># Check constraints can go on the column SQL here</span>
            <span class="n">db_params</span> <span class="o">=</span> <span class="n">field</span><span class="o">.</span><span class="n">db_parameters</span><span class="p">(</span><span class="n">connection</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">connection</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">db_params</span><span class="p">[</span><span class="s1">&#39;check&#39;</span><span class="p">]:</span>
                <span class="n">definition</span> <span class="o">+=</span> <span class="s2">&quot; &quot;</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">sql_check_constraint</span> <span class="o">%</span> <span class="n">db_params</span>
            <span class="c1"># Autoincrement SQL (for backends with inline variant)</span>
            <span class="n">col_type_suffix</span> <span class="o">=</span> <span class="n">field</span><span class="o">.</span><span class="n">db_type_suffix</span><span class="p">(</span><span class="n">connection</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">connection</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">col_type_suffix</span><span class="p">:</span>
                <span class="n">definition</span> <span class="o">+=</span> <span class="s2">&quot; </span><span class="si">%s</span><span class="s2">&quot;</span> <span class="o">%</span> <span class="n">col_type_suffix</span>
            <span class="n">params</span><span class="o">.</span><span class="n">extend</span><span class="p">(</span><span class="n">extra_params</span><span class="p">)</span>
            <span class="c1"># FK</span>
            <span class="k">if</span> <span class="n">field</span><span class="o">.</span><span class="n">remote_field</span> <span class="ow">and</span> <span class="n">field</span><span class="o">.</span><span class="n">db_constraint</span><span class="p">:</span>
                <span class="n">to_table</span> <span class="o">=</span> <span class="n">field</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">db_table</span>
                <span class="n">to_column</span> <span class="o">=</span> <span class="n">field</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">get_field</span><span class="p">(</span><span class="n">field</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">field_name</span><span class="p">)</span><span class="o">.</span><span class="n">column</span>
                <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">sql_create_inline_fk</span><span class="p">:</span>
                    <span class="n">definition</span> <span class="o">+=</span> <span class="s2">&quot; &quot;</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">sql_create_inline_fk</span> <span class="o">%</span> <span class="p">{</span>
                        <span class="s2">&quot;to_table&quot;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">quote_name</span><span class="p">(</span><span class="n">to_table</span><span class="p">),</span>
                        <span class="s2">&quot;to_column&quot;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">quote_name</span><span class="p">(</span><span class="n">to_column</span><span class="p">),</span>
                    <span class="p">}</span>
                <span class="k">elif</span> <span class="bp">self</span><span class="o">.</span><span class="n">connection</span><span class="o">.</span><span class="n">features</span><span class="o">.</span><span class="n">supports_foreign_keys</span><span class="p">:</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">deferred_sql</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_create_fk_sql</span><span class="p">(</span><span class="n">model</span><span class="p">,</span> <span class="n">field</span><span class="p">,</span> <span class="s2">&quot;_fk_</span><span class="si">%(to_table)s</span><span class="s2">_</span><span class="si">%(to_column)s</span><span class="s2">&quot;</span><span class="p">))</span>
            <span class="c1"># Add the SQL to our big list</span>
            <span class="n">column_sqls</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="s2">&quot;</span><span class="si">%s</span><span class="s2"> </span><span class="si">%s</span><span class="s2">&quot;</span> <span class="o">%</span> <span class="p">(</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">quote_name</span><span class="p">(</span><span class="n">field</span><span class="o">.</span><span class="n">column</span><span class="p">),</span>
                <span class="n">definition</span><span class="p">,</span>
            <span class="p">))</span>
            <span class="c1"># Autoincrement SQL (for backends with post table definition variant)</span>
            <span class="k">if</span> <span class="n">field</span><span class="o">.</span><span class="n">get_internal_type</span><span class="p">()</span> <span class="ow">in</span> <span class="p">(</span><span class="s2">&quot;AutoField&quot;</span><span class="p">,</span> <span class="s2">&quot;BigAutoField&quot;</span><span class="p">):</span>
                <span class="n">autoinc_sql</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">connection</span><span class="o">.</span><span class="n">ops</span><span class="o">.</span><span class="n">autoinc_sql</span><span class="p">(</span><span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">db_table</span><span class="p">,</span> <span class="n">field</span><span class="o">.</span><span class="n">column</span><span class="p">)</span>
                <span class="k">if</span> <span class="n">autoinc_sql</span><span class="p">:</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">deferred_sql</span><span class="o">.</span><span class="n">extend</span><span class="p">(</span><span class="n">autoinc_sql</span><span class="p">)</span>

        <span class="c1"># Add any unique_togethers (always deferred, as some fields might be</span>
        <span class="c1"># created afterwards, like geometry fields with some backends)</span>
        <span class="k">for</span> <span class="n">fields</span> <span class="ow">in</span> <span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">unique_together</span><span class="p">:</span>
            <span class="n">columns</span> <span class="o">=</span> <span class="p">[</span><span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">get_field</span><span class="p">(</span><span class="n">field</span><span class="p">)</span><span class="o">.</span><span class="n">column</span> <span class="k">for</span> <span class="n">field</span> <span class="ow">in</span> <span class="n">fields</span><span class="p">]</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">deferred_sql</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_create_unique_sql</span><span class="p">(</span><span class="n">model</span><span class="p">,</span> <span class="n">columns</span><span class="p">))</span>
        <span class="n">constraints</span> <span class="o">=</span> <span class="p">[</span><span class="n">constraint</span><span class="o">.</span><span class="n">constraint_sql</span><span class="p">(</span><span class="n">model</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span> <span class="k">for</span> <span class="n">constraint</span> <span class="ow">in</span> <span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">constraints</span><span class="p">]</span>
        <span class="c1"># Make the table</span>
        <span class="n">sql</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">sql_create_table</span> <span class="o">%</span> <span class="p">{</span>
            <span class="s2">&quot;table&quot;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">quote_name</span><span class="p">(</span><span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">db_table</span><span class="p">),</span>
            <span class="s2">&quot;definition&quot;</span><span class="p">:</span> <span class="s2">&quot;, &quot;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">constraint</span> <span class="k">for</span> <span class="n">constraint</span> <span class="ow">in</span> <span class="p">(</span><span class="o">*</span><span class="n">column_sqls</span><span class="p">,</span> <span class="o">*</span><span class="n">constraints</span><span class="p">)</span> <span class="k">if</span> <span class="n">constraint</span><span class="p">),</span>
        <span class="p">}</span>
        <span class="k">if</span> <span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">db_tablespace</span><span class="p">:</span>
            <span class="n">tablespace_sql</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">connection</span><span class="o">.</span><span class="n">ops</span><span class="o">.</span><span class="n">tablespace_sql</span><span class="p">(</span><span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">db_tablespace</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">tablespace_sql</span><span class="p">:</span>
                <span class="n">sql</span> <span class="o">+=</span> <span class="s1">&#39; &#39;</span> <span class="o">+</span> <span class="n">tablespace_sql</span>
        <span class="c1"># Prevent using [] as params, in the case a literal &#39;%&#39; is used in the definition</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="n">sql</span><span class="p">,</span> <span class="n">params</span> <span class="ow">or</span> <span class="kc">None</span><span class="p">)</span>

        <span class="c1"># Add any field index and index_together&#39;s (deferred as SQLite _remake_table needs it)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">deferred_sql</span><span class="o">.</span><span class="n">extend</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_model_indexes_sql</span><span class="p">(</span><span class="n">model</span><span class="p">))</span>

        <span class="c1"># Make M2M tables</span>
        <span class="k">for</span> <span class="n">field</span> <span class="ow">in</span> <span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">local_many_to_many</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">field</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">through</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">auto_created</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">create_model</span><span class="p">(</span><span class="n">field</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">through</span><span class="p">)</span></div>

<div class="viewcode-block" id="BaseDatabaseSchemaEditor.delete_model"><a class="viewcode-back" href="../../../../../ref/schema-editor.html#django.db.backends.base.schema.BaseDatabaseSchemaEditor.delete_model">[docs]</a>    <span class="k">def</span> <span class="nf">delete_model</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">model</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Delete a model from the database.&quot;&quot;&quot;</span>
        <span class="c1"># Handle auto-created intermediary models</span>
        <span class="k">for</span> <span class="n">field</span> <span class="ow">in</span> <span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">local_many_to_many</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">field</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">through</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">auto_created</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">delete_model</span><span class="p">(</span><span class="n">field</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">through</span><span class="p">)</span>

        <span class="c1"># Delete the table</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">sql_delete_table</span> <span class="o">%</span> <span class="p">{</span>
            <span class="s2">&quot;table&quot;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">quote_name</span><span class="p">(</span><span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">db_table</span><span class="p">),</span>
        <span class="p">})</span>
        <span class="c1"># Remove all deferred statements referencing the deleted table.</span>
        <span class="k">for</span> <span class="n">sql</span> <span class="ow">in</span> <span class="nb">list</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">deferred_sql</span><span class="p">):</span>
            <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">sql</span><span class="p">,</span> <span class="n">Statement</span><span class="p">)</span> <span class="ow">and</span> <span class="n">sql</span><span class="o">.</span><span class="n">references_table</span><span class="p">(</span><span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">db_table</span><span class="p">):</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">deferred_sql</span><span class="o">.</span><span class="n">remove</span><span class="p">(</span><span class="n">sql</span><span class="p">)</span></div>

<div class="viewcode-block" id="BaseDatabaseSchemaEditor.add_index"><a class="viewcode-back" href="../../../../../ref/schema-editor.html#django.db.backends.base.schema.BaseDatabaseSchemaEditor.add_index">[docs]</a>    <span class="k">def</span> <span class="nf">add_index</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">model</span><span class="p">,</span> <span class="n">index</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Add an index on a model.&quot;&quot;&quot;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="n">index</span><span class="o">.</span><span class="n">create_sql</span><span class="p">(</span><span class="n">model</span><span class="p">,</span> <span class="bp">self</span><span class="p">),</span> <span class="n">params</span><span class="o">=</span><span class="kc">None</span><span class="p">)</span></div>

<div class="viewcode-block" id="BaseDatabaseSchemaEditor.remove_index"><a class="viewcode-back" href="../../../../../ref/schema-editor.html#django.db.backends.base.schema.BaseDatabaseSchemaEditor.remove_index">[docs]</a>    <span class="k">def</span> <span class="nf">remove_index</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">model</span><span class="p">,</span> <span class="n">index</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Remove an index from a model.&quot;&quot;&quot;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="n">index</span><span class="o">.</span><span class="n">remove_sql</span><span class="p">(</span><span class="n">model</span><span class="p">,</span> <span class="bp">self</span><span class="p">))</span></div>

<div class="viewcode-block" id="BaseDatabaseSchemaEditor.add_constraint"><a class="viewcode-back" href="../../../../../ref/schema-editor.html#django.db.backends.base.schema.BaseDatabaseSchemaEditor.add_constraint">[docs]</a>    <span class="k">def</span> <span class="nf">add_constraint</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">model</span><span class="p">,</span> <span class="n">constraint</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Add a check constraint to a model.&quot;&quot;&quot;</span>
        <span class="n">sql</span> <span class="o">=</span> <span class="n">constraint</span><span class="o">.</span><span class="n">create_sql</span><span class="p">(</span><span class="n">model</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">sql</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="n">sql</span><span class="p">)</span></div>

<div class="viewcode-block" id="BaseDatabaseSchemaEditor.remove_constraint"><a class="viewcode-back" href="../../../../../ref/schema-editor.html#django.db.backends.base.schema.BaseDatabaseSchemaEditor.remove_constraint">[docs]</a>    <span class="k">def</span> <span class="nf">remove_constraint</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">model</span><span class="p">,</span> <span class="n">constraint</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Remove a check constraint from a model.&quot;&quot;&quot;</span>
        <span class="n">sql</span> <span class="o">=</span> <span class="n">constraint</span><span class="o">.</span><span class="n">remove_sql</span><span class="p">(</span><span class="n">model</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">sql</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="n">sql</span><span class="p">)</span></div>

<div class="viewcode-block" id="BaseDatabaseSchemaEditor.alter_unique_together"><a class="viewcode-back" href="../../../../../ref/schema-editor.html#django.db.backends.base.schema.BaseDatabaseSchemaEditor.alter_unique_together">[docs]</a>    <span class="k">def</span> <span class="nf">alter_unique_together</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">model</span><span class="p">,</span> <span class="n">old_unique_together</span><span class="p">,</span> <span class="n">new_unique_together</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Deal with a model changing its unique_together. The input</span>
<span class="sd">        unique_togethers must be doubly-nested, not the single-nested</span>
<span class="sd">        [&quot;foo&quot;, &quot;bar&quot;] format.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">olds</span> <span class="o">=</span> <span class="p">{</span><span class="nb">tuple</span><span class="p">(</span><span class="n">fields</span><span class="p">)</span> <span class="k">for</span> <span class="n">fields</span> <span class="ow">in</span> <span class="n">old_unique_together</span><span class="p">}</span>
        <span class="n">news</span> <span class="o">=</span> <span class="p">{</span><span class="nb">tuple</span><span class="p">(</span><span class="n">fields</span><span class="p">)</span> <span class="k">for</span> <span class="n">fields</span> <span class="ow">in</span> <span class="n">new_unique_together</span><span class="p">}</span>
        <span class="c1"># Deleted uniques</span>
        <span class="k">for</span> <span class="n">fields</span> <span class="ow">in</span> <span class="n">olds</span><span class="o">.</span><span class="n">difference</span><span class="p">(</span><span class="n">news</span><span class="p">):</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_delete_composed_index</span><span class="p">(</span><span class="n">model</span><span class="p">,</span> <span class="n">fields</span><span class="p">,</span> <span class="p">{</span><span class="s1">&#39;unique&#39;</span><span class="p">:</span> <span class="kc">True</span><span class="p">},</span> <span class="bp">self</span><span class="o">.</span><span class="n">sql_delete_unique</span><span class="p">)</span>
        <span class="c1"># Created uniques</span>
        <span class="k">for</span> <span class="n">fields</span> <span class="ow">in</span> <span class="n">news</span><span class="o">.</span><span class="n">difference</span><span class="p">(</span><span class="n">olds</span><span class="p">):</span>
            <span class="n">columns</span> <span class="o">=</span> <span class="p">[</span><span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">get_field</span><span class="p">(</span><span class="n">field</span><span class="p">)</span><span class="o">.</span><span class="n">column</span> <span class="k">for</span> <span class="n">field</span> <span class="ow">in</span> <span class="n">fields</span><span class="p">]</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_create_unique_sql</span><span class="p">(</span><span class="n">model</span><span class="p">,</span> <span class="n">columns</span><span class="p">))</span></div>

<div class="viewcode-block" id="BaseDatabaseSchemaEditor.alter_index_together"><a class="viewcode-back" href="../../../../../ref/schema-editor.html#django.db.backends.base.schema.BaseDatabaseSchemaEditor.alter_index_together">[docs]</a>    <span class="k">def</span> <span class="nf">alter_index_together</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">model</span><span class="p">,</span> <span class="n">old_index_together</span><span class="p">,</span> <span class="n">new_index_together</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Deal with a model changing its index_together. The input</span>
<span class="sd">        index_togethers must be doubly-nested, not the single-nested</span>
<span class="sd">        [&quot;foo&quot;, &quot;bar&quot;] format.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">olds</span> <span class="o">=</span> <span class="p">{</span><span class="nb">tuple</span><span class="p">(</span><span class="n">fields</span><span class="p">)</span> <span class="k">for</span> <span class="n">fields</span> <span class="ow">in</span> <span class="n">old_index_together</span><span class="p">}</span>
        <span class="n">news</span> <span class="o">=</span> <span class="p">{</span><span class="nb">tuple</span><span class="p">(</span><span class="n">fields</span><span class="p">)</span> <span class="k">for</span> <span class="n">fields</span> <span class="ow">in</span> <span class="n">new_index_together</span><span class="p">}</span>
        <span class="c1"># Deleted indexes</span>
        <span class="k">for</span> <span class="n">fields</span> <span class="ow">in</span> <span class="n">olds</span><span class="o">.</span><span class="n">difference</span><span class="p">(</span><span class="n">news</span><span class="p">):</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_delete_composed_index</span><span class="p">(</span><span class="n">model</span><span class="p">,</span> <span class="n">fields</span><span class="p">,</span> <span class="p">{</span><span class="s1">&#39;index&#39;</span><span class="p">:</span> <span class="kc">True</span><span class="p">},</span> <span class="bp">self</span><span class="o">.</span><span class="n">sql_delete_index</span><span class="p">)</span>
        <span class="c1"># Created indexes</span>
        <span class="k">for</span> <span class="n">field_names</span> <span class="ow">in</span> <span class="n">news</span><span class="o">.</span><span class="n">difference</span><span class="p">(</span><span class="n">olds</span><span class="p">):</span>
            <span class="n">fields</span> <span class="o">=</span> <span class="p">[</span><span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">get_field</span><span class="p">(</span><span class="n">field</span><span class="p">)</span> <span class="k">for</span> <span class="n">field</span> <span class="ow">in</span> <span class="n">field_names</span><span class="p">]</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_create_index_sql</span><span class="p">(</span><span class="n">model</span><span class="p">,</span> <span class="n">fields</span><span class="p">,</span> <span class="n">suffix</span><span class="o">=</span><span class="s2">&quot;_idx&quot;</span><span class="p">))</span></div>

    <span class="k">def</span> <span class="nf">_delete_composed_index</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">model</span><span class="p">,</span> <span class="n">fields</span><span class="p">,</span> <span class="n">constraint_kwargs</span><span class="p">,</span> <span class="n">sql</span><span class="p">):</span>
        <span class="n">meta_constraint_names</span> <span class="o">=</span> <span class="p">{</span><span class="n">constraint</span><span class="o">.</span><span class="n">name</span> <span class="k">for</span> <span class="n">constraint</span> <span class="ow">in</span> <span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">constraints</span><span class="p">}</span>
        <span class="n">meta_index_names</span> <span class="o">=</span> <span class="p">{</span><span class="n">constraint</span><span class="o">.</span><span class="n">name</span> <span class="k">for</span> <span class="n">constraint</span> <span class="ow">in</span> <span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">indexes</span><span class="p">}</span>
        <span class="n">columns</span> <span class="o">=</span> <span class="p">[</span><span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">get_field</span><span class="p">(</span><span class="n">field</span><span class="p">)</span><span class="o">.</span><span class="n">column</span> <span class="k">for</span> <span class="n">field</span> <span class="ow">in</span> <span class="n">fields</span><span class="p">]</span>
        <span class="n">constraint_names</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_constraint_names</span><span class="p">(</span>
            <span class="n">model</span><span class="p">,</span> <span class="n">columns</span><span class="p">,</span> <span class="n">exclude</span><span class="o">=</span><span class="n">meta_constraint_names</span> <span class="o">|</span> <span class="n">meta_index_names</span><span class="p">,</span>
            <span class="o">**</span><span class="n">constraint_kwargs</span>
        <span class="p">)</span>
        <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">constraint_names</span><span class="p">)</span> <span class="o">!=</span> <span class="mi">1</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;Found wrong number (</span><span class="si">%s</span><span class="s2">) of constraints for </span><span class="si">%s</span><span class="s2">(</span><span class="si">%s</span><span class="s2">)&quot;</span> <span class="o">%</span> <span class="p">(</span>
                <span class="nb">len</span><span class="p">(</span><span class="n">constraint_names</span><span class="p">),</span>
                <span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">db_table</span><span class="p">,</span>
                <span class="s2">&quot;, &quot;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">columns</span><span class="p">),</span>
            <span class="p">))</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_delete_constraint_sql</span><span class="p">(</span><span class="n">sql</span><span class="p">,</span> <span class="n">model</span><span class="p">,</span> <span class="n">constraint_names</span><span class="p">[</span><span class="mi">0</span><span class="p">]))</span>

<div class="viewcode-block" id="BaseDatabaseSchemaEditor.alter_db_table"><a class="viewcode-back" href="../../../../../ref/schema-editor.html#django.db.backends.base.schema.BaseDatabaseSchemaEditor.alter_db_table">[docs]</a>    <span class="k">def</span> <span class="nf">alter_db_table</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">model</span><span class="p">,</span> <span class="n">old_db_table</span><span class="p">,</span> <span class="n">new_db_table</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Rename the table a model points to.&quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="p">(</span><span class="n">old_db_table</span> <span class="o">==</span> <span class="n">new_db_table</span> <span class="ow">or</span>
            <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">connection</span><span class="o">.</span><span class="n">features</span><span class="o">.</span><span class="n">ignores_table_name_case</span> <span class="ow">and</span>
                <span class="n">old_db_table</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span> <span class="o">==</span> <span class="n">new_db_table</span><span class="o">.</span><span class="n">lower</span><span class="p">())):</span>
            <span class="k">return</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">sql_rename_table</span> <span class="o">%</span> <span class="p">{</span>
            <span class="s2">&quot;old_table&quot;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">quote_name</span><span class="p">(</span><span class="n">old_db_table</span><span class="p">),</span>
            <span class="s2">&quot;new_table&quot;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">quote_name</span><span class="p">(</span><span class="n">new_db_table</span><span class="p">),</span>
        <span class="p">})</span>
        <span class="c1"># Rename all references to the old table name.</span>
        <span class="k">for</span> <span class="n">sql</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">deferred_sql</span><span class="p">:</span>
            <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">sql</span><span class="p">,</span> <span class="n">Statement</span><span class="p">):</span>
                <span class="n">sql</span><span class="o">.</span><span class="n">rename_table_references</span><span class="p">(</span><span class="n">old_db_table</span><span class="p">,</span> <span class="n">new_db_table</span><span class="p">)</span></div>

<div class="viewcode-block" id="BaseDatabaseSchemaEditor.alter_db_tablespace"><a class="viewcode-back" href="../../../../../ref/schema-editor.html#django.db.backends.base.schema.BaseDatabaseSchemaEditor.alter_db_tablespace">[docs]</a>    <span class="k">def</span> <span class="nf">alter_db_tablespace</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">model</span><span class="p">,</span> <span class="n">old_db_tablespace</span><span class="p">,</span> <span class="n">new_db_tablespace</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Move a model&#39;s table between tablespaces.&quot;&quot;&quot;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">sql_retablespace_table</span> <span class="o">%</span> <span class="p">{</span>
            <span class="s2">&quot;table&quot;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">quote_name</span><span class="p">(</span><span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">db_table</span><span class="p">),</span>
            <span class="s2">&quot;old_tablespace&quot;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">quote_name</span><span class="p">(</span><span class="n">old_db_tablespace</span><span class="p">),</span>
            <span class="s2">&quot;new_tablespace&quot;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">quote_name</span><span class="p">(</span><span class="n">new_db_tablespace</span><span class="p">),</span>
        <span class="p">})</span></div>

<div class="viewcode-block" id="BaseDatabaseSchemaEditor.add_field"><a class="viewcode-back" href="../../../../../ref/schema-editor.html#django.db.backends.base.schema.BaseDatabaseSchemaEditor.add_field">[docs]</a>    <span class="k">def</span> <span class="nf">add_field</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">model</span><span class="p">,</span> <span class="n">field</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Create a field on a model. Usually involves adding a column, but may</span>
<span class="sd">        involve adding a table instead (for M2M fields).</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="c1"># Special-case implicit M2M tables</span>
        <span class="k">if</span> <span class="n">field</span><span class="o">.</span><span class="n">many_to_many</span> <span class="ow">and</span> <span class="n">field</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">through</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">auto_created</span><span class="p">:</span>
            <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">create_model</span><span class="p">(</span><span class="n">field</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">through</span><span class="p">)</span>
        <span class="c1"># Get the column&#39;s definition</span>
        <span class="n">definition</span><span class="p">,</span> <span class="n">params</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">column_sql</span><span class="p">(</span><span class="n">model</span><span class="p">,</span> <span class="n">field</span><span class="p">,</span> <span class="n">include_default</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
        <span class="c1"># It might not actually have a column behind it</span>
        <span class="k">if</span> <span class="n">definition</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="k">return</span>
        <span class="c1"># Check constraints can go on the column SQL here</span>
        <span class="n">db_params</span> <span class="o">=</span> <span class="n">field</span><span class="o">.</span><span class="n">db_parameters</span><span class="p">(</span><span class="n">connection</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">connection</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">db_params</span><span class="p">[</span><span class="s1">&#39;check&#39;</span><span class="p">]:</span>
            <span class="n">definition</span> <span class="o">+=</span> <span class="s2">&quot; &quot;</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">sql_check_constraint</span> <span class="o">%</span> <span class="n">db_params</span>
        <span class="c1"># Build the SQL and run it</span>
        <span class="n">sql</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">sql_create_column</span> <span class="o">%</span> <span class="p">{</span>
            <span class="s2">&quot;table&quot;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">quote_name</span><span class="p">(</span><span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">db_table</span><span class="p">),</span>
            <span class="s2">&quot;column&quot;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">quote_name</span><span class="p">(</span><span class="n">field</span><span class="o">.</span><span class="n">column</span><span class="p">),</span>
            <span class="s2">&quot;definition&quot;</span><span class="p">:</span> <span class="n">definition</span><span class="p">,</span>
        <span class="p">}</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="n">sql</span><span class="p">,</span> <span class="n">params</span><span class="p">)</span>
        <span class="c1"># Drop the default if we need to</span>
        <span class="c1"># (Django usually does not use in-database defaults)</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">skip_default</span><span class="p">(</span><span class="n">field</span><span class="p">)</span> <span class="ow">and</span> <span class="bp">self</span><span class="o">.</span><span class="n">effective_default</span><span class="p">(</span><span class="n">field</span><span class="p">)</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">changes_sql</span><span class="p">,</span> <span class="n">params</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_alter_column_default_sql</span><span class="p">(</span><span class="n">model</span><span class="p">,</span> <span class="kc">None</span><span class="p">,</span> <span class="n">field</span><span class="p">,</span> <span class="n">drop</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
            <span class="n">sql</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">sql_alter_column</span> <span class="o">%</span> <span class="p">{</span>
                <span class="s2">&quot;table&quot;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">quote_name</span><span class="p">(</span><span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">db_table</span><span class="p">),</span>
                <span class="s2">&quot;changes&quot;</span><span class="p">:</span> <span class="n">changes_sql</span><span class="p">,</span>
            <span class="p">}</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="n">sql</span><span class="p">,</span> <span class="n">params</span><span class="p">)</span>
        <span class="c1"># Add an index, if required</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">deferred_sql</span><span class="o">.</span><span class="n">extend</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_field_indexes_sql</span><span class="p">(</span><span class="n">model</span><span class="p">,</span> <span class="n">field</span><span class="p">))</span>
        <span class="c1"># Add any FK constraints later</span>
        <span class="k">if</span> <span class="n">field</span><span class="o">.</span><span class="n">remote_field</span> <span class="ow">and</span> <span class="bp">self</span><span class="o">.</span><span class="n">connection</span><span class="o">.</span><span class="n">features</span><span class="o">.</span><span class="n">supports_foreign_keys</span> <span class="ow">and</span> <span class="n">field</span><span class="o">.</span><span class="n">db_constraint</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">deferred_sql</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_create_fk_sql</span><span class="p">(</span><span class="n">model</span><span class="p">,</span> <span class="n">field</span><span class="p">,</span> <span class="s2">&quot;_fk_</span><span class="si">%(to_table)s</span><span class="s2">_</span><span class="si">%(to_column)s</span><span class="s2">&quot;</span><span class="p">))</span>
        <span class="c1"># Reset connection if required</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">connection</span><span class="o">.</span><span class="n">features</span><span class="o">.</span><span class="n">connection_persists_old_columns</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">connection</span><span class="o">.</span><span class="n">close</span><span class="p">()</span></div>

<div class="viewcode-block" id="BaseDatabaseSchemaEditor.remove_field"><a class="viewcode-back" href="../../../../../ref/schema-editor.html#django.db.backends.base.schema.BaseDatabaseSchemaEditor.remove_field">[docs]</a>    <span class="k">def</span> <span class="nf">remove_field</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">model</span><span class="p">,</span> <span class="n">field</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Remove a field from a model. Usually involves deleting a column,</span>
<span class="sd">        but for M2Ms may involve deleting a table.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="c1"># Special-case implicit M2M tables</span>
        <span class="k">if</span> <span class="n">field</span><span class="o">.</span><span class="n">many_to_many</span> <span class="ow">and</span> <span class="n">field</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">through</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">auto_created</span><span class="p">:</span>
            <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">delete_model</span><span class="p">(</span><span class="n">field</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">through</span><span class="p">)</span>
        <span class="c1"># It might not actually have a column behind it</span>
        <span class="k">if</span> <span class="n">field</span><span class="o">.</span><span class="n">db_parameters</span><span class="p">(</span><span class="n">connection</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">connection</span><span class="p">)[</span><span class="s1">&#39;type&#39;</span><span class="p">]</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="k">return</span>
        <span class="c1"># Drop any FK constraints, MySQL requires explicit deletion</span>
        <span class="k">if</span> <span class="n">field</span><span class="o">.</span><span class="n">remote_field</span><span class="p">:</span>
            <span class="n">fk_names</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_constraint_names</span><span class="p">(</span><span class="n">model</span><span class="p">,</span> <span class="p">[</span><span class="n">field</span><span class="o">.</span><span class="n">column</span><span class="p">],</span> <span class="n">foreign_key</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
            <span class="k">for</span> <span class="n">fk_name</span> <span class="ow">in</span> <span class="n">fk_names</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_delete_fk_sql</span><span class="p">(</span><span class="n">model</span><span class="p">,</span> <span class="n">fk_name</span><span class="p">))</span>
        <span class="c1"># Delete the column</span>
        <span class="n">sql</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">sql_delete_column</span> <span class="o">%</span> <span class="p">{</span>
            <span class="s2">&quot;table&quot;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">quote_name</span><span class="p">(</span><span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">db_table</span><span class="p">),</span>
            <span class="s2">&quot;column&quot;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">quote_name</span><span class="p">(</span><span class="n">field</span><span class="o">.</span><span class="n">column</span><span class="p">),</span>
        <span class="p">}</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="n">sql</span><span class="p">)</span>
        <span class="c1"># Reset connection if required</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">connection</span><span class="o">.</span><span class="n">features</span><span class="o">.</span><span class="n">connection_persists_old_columns</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">connection</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
        <span class="c1"># Remove all deferred statements referencing the deleted column.</span>
        <span class="k">for</span> <span class="n">sql</span> <span class="ow">in</span> <span class="nb">list</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">deferred_sql</span><span class="p">):</span>
            <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">sql</span><span class="p">,</span> <span class="n">Statement</span><span class="p">)</span> <span class="ow">and</span> <span class="n">sql</span><span class="o">.</span><span class="n">references_column</span><span class="p">(</span><span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">db_table</span><span class="p">,</span> <span class="n">field</span><span class="o">.</span><span class="n">column</span><span class="p">):</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">deferred_sql</span><span class="o">.</span><span class="n">remove</span><span class="p">(</span><span class="n">sql</span><span class="p">)</span></div>

<div class="viewcode-block" id="BaseDatabaseSchemaEditor.alter_field"><a class="viewcode-back" href="../../../../../ref/schema-editor.html#django.db.backends.base.schema.BaseDatabaseSchemaEditor.alter_field">[docs]</a>    <span class="k">def</span> <span class="nf">alter_field</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">model</span><span class="p">,</span> <span class="n">old_field</span><span class="p">,</span> <span class="n">new_field</span><span class="p">,</span> <span class="n">strict</span><span class="o">=</span><span class="kc">False</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Allow a field&#39;s type, uniqueness, nullability, default, column,</span>
<span class="sd">        constraints, etc. to be modified.</span>
<span class="sd">        `old_field` is required to compute the necessary changes.</span>
<span class="sd">        If `strict` is True, raise errors if the old column does not match</span>
<span class="sd">        `old_field` precisely.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="c1"># Ensure this field is even column-based</span>
        <span class="n">old_db_params</span> <span class="o">=</span> <span class="n">old_field</span><span class="o">.</span><span class="n">db_parameters</span><span class="p">(</span><span class="n">connection</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">connection</span><span class="p">)</span>
        <span class="n">old_type</span> <span class="o">=</span> <span class="n">old_db_params</span><span class="p">[</span><span class="s1">&#39;type&#39;</span><span class="p">]</span>
        <span class="n">new_db_params</span> <span class="o">=</span> <span class="n">new_field</span><span class="o">.</span><span class="n">db_parameters</span><span class="p">(</span><span class="n">connection</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">connection</span><span class="p">)</span>
        <span class="n">new_type</span> <span class="o">=</span> <span class="n">new_db_params</span><span class="p">[</span><span class="s1">&#39;type&#39;</span><span class="p">]</span>
        <span class="k">if</span> <span class="p">((</span><span class="n">old_type</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">old_field</span><span class="o">.</span><span class="n">remote_field</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">)</span> <span class="ow">or</span>
                <span class="p">(</span><span class="n">new_type</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">new_field</span><span class="o">.</span><span class="n">remote_field</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">)):</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span>
                <span class="s2">&quot;Cannot alter field </span><span class="si">%s</span><span class="s2"> into </span><span class="si">%s</span><span class="s2"> - they do not properly define &quot;</span>
                <span class="s2">&quot;db_type (are you using a badly-written custom field?)&quot;</span> <span class="o">%</span>
                <span class="p">(</span><span class="n">old_field</span><span class="p">,</span> <span class="n">new_field</span><span class="p">),</span>
            <span class="p">)</span>
        <span class="k">elif</span> <span class="n">old_type</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">new_type</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">and</span> <span class="p">(</span>
                <span class="n">old_field</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">through</span> <span class="ow">and</span> <span class="n">new_field</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">through</span> <span class="ow">and</span>
                <span class="n">old_field</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">through</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">auto_created</span> <span class="ow">and</span>
                <span class="n">new_field</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">through</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">auto_created</span><span class="p">):</span>
            <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_alter_many_to_many</span><span class="p">(</span><span class="n">model</span><span class="p">,</span> <span class="n">old_field</span><span class="p">,</span> <span class="n">new_field</span><span class="p">,</span> <span class="n">strict</span><span class="p">)</span>
        <span class="k">elif</span> <span class="n">old_type</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">new_type</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">and</span> <span class="p">(</span>
                <span class="n">old_field</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">through</span> <span class="ow">and</span> <span class="n">new_field</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">through</span> <span class="ow">and</span>
                <span class="ow">not</span> <span class="n">old_field</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">through</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">auto_created</span> <span class="ow">and</span>
                <span class="ow">not</span> <span class="n">new_field</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">through</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">auto_created</span><span class="p">):</span>
            <span class="c1"># Both sides have through models; this is a no-op.</span>
            <span class="k">return</span>
        <span class="k">elif</span> <span class="n">old_type</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">or</span> <span class="n">new_type</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span>
                <span class="s2">&quot;Cannot alter field </span><span class="si">%s</span><span class="s2"> into </span><span class="si">%s</span><span class="s2"> - they are not compatible types &quot;</span>
                <span class="s2">&quot;(you cannot alter to or from M2M fields, or add or remove &quot;</span>
                <span class="s2">&quot;through= on M2M fields)&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">old_field</span><span class="p">,</span> <span class="n">new_field</span><span class="p">)</span>
            <span class="p">)</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">_alter_field</span><span class="p">(</span><span class="n">model</span><span class="p">,</span> <span class="n">old_field</span><span class="p">,</span> <span class="n">new_field</span><span class="p">,</span> <span class="n">old_type</span><span class="p">,</span> <span class="n">new_type</span><span class="p">,</span>
                          <span class="n">old_db_params</span><span class="p">,</span> <span class="n">new_db_params</span><span class="p">,</span> <span class="n">strict</span><span class="p">)</span></div>

    <span class="k">def</span> <span class="nf">_alter_field</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">model</span><span class="p">,</span> <span class="n">old_field</span><span class="p">,</span> <span class="n">new_field</span><span class="p">,</span> <span class="n">old_type</span><span class="p">,</span> <span class="n">new_type</span><span class="p">,</span>
                     <span class="n">old_db_params</span><span class="p">,</span> <span class="n">new_db_params</span><span class="p">,</span> <span class="n">strict</span><span class="o">=</span><span class="kc">False</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Perform a &quot;physical&quot; (non-ManyToMany) field update.&quot;&quot;&quot;</span>
        <span class="c1"># Drop any FK constraints, we&#39;ll remake them later</span>
        <span class="n">fks_dropped</span> <span class="o">=</span> <span class="nb">set</span><span class="p">()</span>
        <span class="k">if</span> <span class="n">old_field</span><span class="o">.</span><span class="n">remote_field</span> <span class="ow">and</span> <span class="n">old_field</span><span class="o">.</span><span class="n">db_constraint</span><span class="p">:</span>
            <span class="n">fk_names</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_constraint_names</span><span class="p">(</span><span class="n">model</span><span class="p">,</span> <span class="p">[</span><span class="n">old_field</span><span class="o">.</span><span class="n">column</span><span class="p">],</span> <span class="n">foreign_key</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">strict</span> <span class="ow">and</span> <span class="nb">len</span><span class="p">(</span><span class="n">fk_names</span><span class="p">)</span> <span class="o">!=</span> <span class="mi">1</span><span class="p">:</span>
                <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;Found wrong number (</span><span class="si">%s</span><span class="s2">) of foreign key constraints for </span><span class="si">%s</span><span class="s2">.</span><span class="si">%s</span><span class="s2">&quot;</span> <span class="o">%</span> <span class="p">(</span>
                    <span class="nb">len</span><span class="p">(</span><span class="n">fk_names</span><span class="p">),</span>
                    <span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">db_table</span><span class="p">,</span>
                    <span class="n">old_field</span><span class="o">.</span><span class="n">column</span><span class="p">,</span>
                <span class="p">))</span>
            <span class="k">for</span> <span class="n">fk_name</span> <span class="ow">in</span> <span class="n">fk_names</span><span class="p">:</span>
                <span class="n">fks_dropped</span><span class="o">.</span><span class="n">add</span><span class="p">((</span><span class="n">old_field</span><span class="o">.</span><span class="n">column</span><span class="p">,))</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_delete_fk_sql</span><span class="p">(</span><span class="n">model</span><span class="p">,</span> <span class="n">fk_name</span><span class="p">))</span>
        <span class="c1"># Has unique been removed?</span>
        <span class="k">if</span> <span class="n">old_field</span><span class="o">.</span><span class="n">unique</span> <span class="ow">and</span> <span class="p">(</span><span class="ow">not</span> <span class="n">new_field</span><span class="o">.</span><span class="n">unique</span> <span class="ow">or</span> <span class="bp">self</span><span class="o">.</span><span class="n">_field_became_primary_key</span><span class="p">(</span><span class="n">old_field</span><span class="p">,</span> <span class="n">new_field</span><span class="p">)):</span>
            <span class="c1"># Find the unique constraint for this field</span>
            <span class="n">meta_constraint_names</span> <span class="o">=</span> <span class="p">{</span><span class="n">constraint</span><span class="o">.</span><span class="n">name</span> <span class="k">for</span> <span class="n">constraint</span> <span class="ow">in</span> <span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">constraints</span><span class="p">}</span>
            <span class="n">constraint_names</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_constraint_names</span><span class="p">(</span>
                <span class="n">model</span><span class="p">,</span> <span class="p">[</span><span class="n">old_field</span><span class="o">.</span><span class="n">column</span><span class="p">],</span> <span class="n">unique</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> <span class="n">primary_key</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span>
                <span class="n">exclude</span><span class="o">=</span><span class="n">meta_constraint_names</span><span class="p">,</span>
            <span class="p">)</span>
            <span class="k">if</span> <span class="n">strict</span> <span class="ow">and</span> <span class="nb">len</span><span class="p">(</span><span class="n">constraint_names</span><span class="p">)</span> <span class="o">!=</span> <span class="mi">1</span><span class="p">:</span>
                <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;Found wrong number (</span><span class="si">%s</span><span class="s2">) of unique constraints for </span><span class="si">%s</span><span class="s2">.</span><span class="si">%s</span><span class="s2">&quot;</span> <span class="o">%</span> <span class="p">(</span>
                    <span class="nb">len</span><span class="p">(</span><span class="n">constraint_names</span><span class="p">),</span>
                    <span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">db_table</span><span class="p">,</span>
                    <span class="n">old_field</span><span class="o">.</span><span class="n">column</span><span class="p">,</span>
                <span class="p">))</span>
            <span class="k">for</span> <span class="n">constraint_name</span> <span class="ow">in</span> <span class="n">constraint_names</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_delete_unique_sql</span><span class="p">(</span><span class="n">model</span><span class="p">,</span> <span class="n">constraint_name</span><span class="p">))</span>
        <span class="c1"># Drop incoming FK constraints if the field is a primary key or unique,</span>
        <span class="c1"># which might be a to_field target, and things are going to change.</span>
        <span class="n">drop_foreign_keys</span> <span class="o">=</span> <span class="p">(</span>
            <span class="p">(</span>
                <span class="p">(</span><span class="n">old_field</span><span class="o">.</span><span class="n">primary_key</span> <span class="ow">and</span> <span class="n">new_field</span><span class="o">.</span><span class="n">primary_key</span><span class="p">)</span> <span class="ow">or</span>
                <span class="p">(</span><span class="n">old_field</span><span class="o">.</span><span class="n">unique</span> <span class="ow">and</span> <span class="n">new_field</span><span class="o">.</span><span class="n">unique</span><span class="p">)</span>
            <span class="p">)</span> <span class="ow">and</span> <span class="n">old_type</span> <span class="o">!=</span> <span class="n">new_type</span>
        <span class="p">)</span>
        <span class="k">if</span> <span class="n">drop_foreign_keys</span><span class="p">:</span>
            <span class="c1"># &#39;_meta.related_field&#39; also contains M2M reverse fields, these</span>
            <span class="c1"># will be filtered out</span>
            <span class="k">for</span> <span class="n">_old_rel</span><span class="p">,</span> <span class="n">new_rel</span> <span class="ow">in</span> <span class="n">_related_non_m2m_objects</span><span class="p">(</span><span class="n">old_field</span><span class="p">,</span> <span class="n">new_field</span><span class="p">):</span>
                <span class="n">rel_fk_names</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_constraint_names</span><span class="p">(</span>
                    <span class="n">new_rel</span><span class="o">.</span><span class="n">related_model</span><span class="p">,</span> <span class="p">[</span><span class="n">new_rel</span><span class="o">.</span><span class="n">field</span><span class="o">.</span><span class="n">column</span><span class="p">],</span> <span class="n">foreign_key</span><span class="o">=</span><span class="kc">True</span>
                <span class="p">)</span>
                <span class="k">for</span> <span class="n">fk_name</span> <span class="ow">in</span> <span class="n">rel_fk_names</span><span class="p">:</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_delete_fk_sql</span><span class="p">(</span><span class="n">new_rel</span><span class="o">.</span><span class="n">related_model</span><span class="p">,</span> <span class="n">fk_name</span><span class="p">))</span>
        <span class="c1"># Removed an index? (no strict check, as multiple indexes are possible)</span>
        <span class="c1"># Remove indexes if db_index switched to False or a unique constraint</span>
        <span class="c1"># will now be used in lieu of an index. The following lines from the</span>
        <span class="c1"># truth table show all True cases; the rest are False:</span>
        <span class="c1">#</span>
        <span class="c1"># old_field.db_index | old_field.unique | new_field.db_index | new_field.unique</span>
        <span class="c1"># ------------------------------------------------------------------------------</span>
        <span class="c1"># True               | False            | False              | False</span>
        <span class="c1"># True               | False            | False              | True</span>
        <span class="c1"># True               | False            | True               | True</span>
        <span class="k">if</span> <span class="n">old_field</span><span class="o">.</span><span class="n">db_index</span> <span class="ow">and</span> <span class="ow">not</span> <span class="n">old_field</span><span class="o">.</span><span class="n">unique</span> <span class="ow">and</span> <span class="p">(</span><span class="ow">not</span> <span class="n">new_field</span><span class="o">.</span><span class="n">db_index</span> <span class="ow">or</span> <span class="n">new_field</span><span class="o">.</span><span class="n">unique</span><span class="p">):</span>
            <span class="c1"># Find the index for this field</span>
            <span class="n">meta_index_names</span> <span class="o">=</span> <span class="p">{</span><span class="n">index</span><span class="o">.</span><span class="n">name</span> <span class="k">for</span> <span class="n">index</span> <span class="ow">in</span> <span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">indexes</span><span class="p">}</span>
            <span class="c1"># Retrieve only BTREE indexes since this is what&#39;s created with</span>
            <span class="c1"># db_index=True.</span>
            <span class="n">index_names</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_constraint_names</span><span class="p">(</span>
                <span class="n">model</span><span class="p">,</span> <span class="p">[</span><span class="n">old_field</span><span class="o">.</span><span class="n">column</span><span class="p">],</span> <span class="n">index</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> <span class="n">type_</span><span class="o">=</span><span class="n">Index</span><span class="o">.</span><span class="n">suffix</span><span class="p">,</span>
                <span class="n">exclude</span><span class="o">=</span><span class="n">meta_index_names</span><span class="p">,</span>
            <span class="p">)</span>
            <span class="k">for</span> <span class="n">index_name</span> <span class="ow">in</span> <span class="n">index_names</span><span class="p">:</span>
                <span class="c1"># The only way to check if an index was created with</span>
                <span class="c1"># db_index=True or with Index([&#39;field&#39;], name=&#39;foo&#39;)</span>
                <span class="c1"># is to look at its name (refs #28053).</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_delete_index_sql</span><span class="p">(</span><span class="n">model</span><span class="p">,</span> <span class="n">index_name</span><span class="p">))</span>
        <span class="c1"># Change check constraints?</span>
        <span class="k">if</span> <span class="n">old_db_params</span><span class="p">[</span><span class="s1">&#39;check&#39;</span><span class="p">]</span> <span class="o">!=</span> <span class="n">new_db_params</span><span class="p">[</span><span class="s1">&#39;check&#39;</span><span class="p">]</span> <span class="ow">and</span> <span class="n">old_db_params</span><span class="p">[</span><span class="s1">&#39;check&#39;</span><span class="p">]:</span>
            <span class="n">meta_constraint_names</span> <span class="o">=</span> <span class="p">{</span><span class="n">constraint</span><span class="o">.</span><span class="n">name</span> <span class="k">for</span> <span class="n">constraint</span> <span class="ow">in</span> <span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">constraints</span><span class="p">}</span>
            <span class="n">constraint_names</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_constraint_names</span><span class="p">(</span>
                <span class="n">model</span><span class="p">,</span> <span class="p">[</span><span class="n">old_field</span><span class="o">.</span><span class="n">column</span><span class="p">],</span> <span class="n">check</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span>
                <span class="n">exclude</span><span class="o">=</span><span class="n">meta_constraint_names</span><span class="p">,</span>
            <span class="p">)</span>
            <span class="k">if</span> <span class="n">strict</span> <span class="ow">and</span> <span class="nb">len</span><span class="p">(</span><span class="n">constraint_names</span><span class="p">)</span> <span class="o">!=</span> <span class="mi">1</span><span class="p">:</span>
                <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;Found wrong number (</span><span class="si">%s</span><span class="s2">) of check constraints for </span><span class="si">%s</span><span class="s2">.</span><span class="si">%s</span><span class="s2">&quot;</span> <span class="o">%</span> <span class="p">(</span>
                    <span class="nb">len</span><span class="p">(</span><span class="n">constraint_names</span><span class="p">),</span>
                    <span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">db_table</span><span class="p">,</span>
                    <span class="n">old_field</span><span class="o">.</span><span class="n">column</span><span class="p">,</span>
                <span class="p">))</span>
            <span class="k">for</span> <span class="n">constraint_name</span> <span class="ow">in</span> <span class="n">constraint_names</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_delete_check_sql</span><span class="p">(</span><span class="n">model</span><span class="p">,</span> <span class="n">constraint_name</span><span class="p">))</span>
        <span class="c1"># Have they renamed the column?</span>
        <span class="k">if</span> <span class="n">old_field</span><span class="o">.</span><span class="n">column</span> <span class="o">!=</span> <span class="n">new_field</span><span class="o">.</span><span class="n">column</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_rename_field_sql</span><span class="p">(</span><span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">db_table</span><span class="p">,</span> <span class="n">old_field</span><span class="p">,</span> <span class="n">new_field</span><span class="p">,</span> <span class="n">new_type</span><span class="p">))</span>
            <span class="c1"># Rename all references to the renamed column.</span>
            <span class="k">for</span> <span class="n">sql</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">deferred_sql</span><span class="p">:</span>
                <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">sql</span><span class="p">,</span> <span class="n">Statement</span><span class="p">):</span>
                    <span class="n">sql</span><span class="o">.</span><span class="n">rename_column_references</span><span class="p">(</span><span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">db_table</span><span class="p">,</span> <span class="n">old_field</span><span class="o">.</span><span class="n">column</span><span class="p">,</span> <span class="n">new_field</span><span class="o">.</span><span class="n">column</span><span class="p">)</span>
        <span class="c1"># Next, start accumulating actions to do</span>
        <span class="n">actions</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="n">null_actions</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="n">post_actions</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="c1"># Type change?</span>
        <span class="k">if</span> <span class="n">old_type</span> <span class="o">!=</span> <span class="n">new_type</span><span class="p">:</span>
            <span class="n">fragment</span><span class="p">,</span> <span class="n">other_actions</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_alter_column_type_sql</span><span class="p">(</span><span class="n">model</span><span class="p">,</span> <span class="n">old_field</span><span class="p">,</span> <span class="n">new_field</span><span class="p">,</span> <span class="n">new_type</span><span class="p">)</span>
            <span class="n">actions</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">fragment</span><span class="p">)</span>
            <span class="n">post_actions</span><span class="o">.</span><span class="n">extend</span><span class="p">(</span><span class="n">other_actions</span><span class="p">)</span>
        <span class="c1"># When changing a column NULL constraint to NOT NULL with a given</span>
        <span class="c1"># default value, we need to perform 4 steps:</span>
        <span class="c1">#  1. Add a default for new incoming writes</span>
        <span class="c1">#  2. Update existing NULL rows with new default</span>
        <span class="c1">#  3. Replace NULL constraint with NOT NULL</span>
        <span class="c1">#  4. Drop the default again.</span>
        <span class="c1"># Default change?</span>
        <span class="n">old_default</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">effective_default</span><span class="p">(</span><span class="n">old_field</span><span class="p">)</span>
        <span class="n">new_default</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">effective_default</span><span class="p">(</span><span class="n">new_field</span><span class="p">)</span>
        <span class="n">needs_database_default</span> <span class="o">=</span> <span class="p">(</span>
            <span class="n">old_field</span><span class="o">.</span><span class="n">null</span> <span class="ow">and</span>
            <span class="ow">not</span> <span class="n">new_field</span><span class="o">.</span><span class="n">null</span> <span class="ow">and</span>
            <span class="n">old_default</span> <span class="o">!=</span> <span class="n">new_default</span> <span class="ow">and</span>
            <span class="n">new_default</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span> <span class="ow">and</span>
            <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">skip_default</span><span class="p">(</span><span class="n">new_field</span><span class="p">)</span>
        <span class="p">)</span>
        <span class="k">if</span> <span class="n">needs_database_default</span><span class="p">:</span>
            <span class="n">actions</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_alter_column_default_sql</span><span class="p">(</span><span class="n">model</span><span class="p">,</span> <span class="n">old_field</span><span class="p">,</span> <span class="n">new_field</span><span class="p">))</span>
        <span class="c1"># Nullability change?</span>
        <span class="k">if</span> <span class="n">old_field</span><span class="o">.</span><span class="n">null</span> <span class="o">!=</span> <span class="n">new_field</span><span class="o">.</span><span class="n">null</span><span class="p">:</span>
            <span class="n">fragment</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_alter_column_null_sql</span><span class="p">(</span><span class="n">model</span><span class="p">,</span> <span class="n">old_field</span><span class="p">,</span> <span class="n">new_field</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">fragment</span><span class="p">:</span>
                <span class="n">null_actions</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">fragment</span><span class="p">)</span>
        <span class="c1"># Only if we have a default and there is a change from NULL to NOT NULL</span>
        <span class="n">four_way_default_alteration</span> <span class="o">=</span> <span class="p">(</span>
            <span class="n">new_field</span><span class="o">.</span><span class="n">has_default</span><span class="p">()</span> <span class="ow">and</span>
            <span class="p">(</span><span class="n">old_field</span><span class="o">.</span><span class="n">null</span> <span class="ow">and</span> <span class="ow">not</span> <span class="n">new_field</span><span class="o">.</span><span class="n">null</span><span class="p">)</span>
        <span class="p">)</span>
        <span class="k">if</span> <span class="n">actions</span> <span class="ow">or</span> <span class="n">null_actions</span><span class="p">:</span>
            <span class="k">if</span> <span class="ow">not</span> <span class="n">four_way_default_alteration</span><span class="p">:</span>
                <span class="c1"># If we don&#39;t have to do a 4-way default alteration we can</span>
                <span class="c1"># directly run a (NOT) NULL alteration</span>
                <span class="n">actions</span> <span class="o">=</span> <span class="n">actions</span> <span class="o">+</span> <span class="n">null_actions</span>
            <span class="c1"># Combine actions together if we can (e.g. postgres)</span>
            <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">connection</span><span class="o">.</span><span class="n">features</span><span class="o">.</span><span class="n">supports_combined_alters</span> <span class="ow">and</span> <span class="n">actions</span><span class="p">:</span>
                <span class="n">sql</span><span class="p">,</span> <span class="n">params</span> <span class="o">=</span> <span class="nb">tuple</span><span class="p">(</span><span class="nb">zip</span><span class="p">(</span><span class="o">*</span><span class="n">actions</span><span class="p">))</span>
                <span class="n">actions</span> <span class="o">=</span> <span class="p">[(</span><span class="s2">&quot;, &quot;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">sql</span><span class="p">),</span> <span class="nb">sum</span><span class="p">(</span><span class="n">params</span><span class="p">,</span> <span class="p">[]))]</span>
            <span class="c1"># Apply those actions</span>
            <span class="k">for</span> <span class="n">sql</span><span class="p">,</span> <span class="n">params</span> <span class="ow">in</span> <span class="n">actions</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">sql_alter_column</span> <span class="o">%</span> <span class="p">{</span>
                        <span class="s2">&quot;table&quot;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">quote_name</span><span class="p">(</span><span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">db_table</span><span class="p">),</span>
                        <span class="s2">&quot;changes&quot;</span><span class="p">:</span> <span class="n">sql</span><span class="p">,</span>
                    <span class="p">},</span>
                    <span class="n">params</span><span class="p">,</span>
                <span class="p">)</span>
            <span class="k">if</span> <span class="n">four_way_default_alteration</span><span class="p">:</span>
                <span class="c1"># Update existing rows with default value</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">sql_update_with_default</span> <span class="o">%</span> <span class="p">{</span>
                        <span class="s2">&quot;table&quot;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">quote_name</span><span class="p">(</span><span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">db_table</span><span class="p">),</span>
                        <span class="s2">&quot;column&quot;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">quote_name</span><span class="p">(</span><span class="n">new_field</span><span class="o">.</span><span class="n">column</span><span class="p">),</span>
                        <span class="s2">&quot;default&quot;</span><span class="p">:</span> <span class="s2">&quot;</span><span class="si">%s</span><span class="s2">&quot;</span><span class="p">,</span>
                    <span class="p">},</span>
                    <span class="p">[</span><span class="n">new_default</span><span class="p">],</span>
                <span class="p">)</span>
                <span class="c1"># Since we didn&#39;t run a NOT NULL change before we need to do it</span>
                <span class="c1"># now</span>
                <span class="k">for</span> <span class="n">sql</span><span class="p">,</span> <span class="n">params</span> <span class="ow">in</span> <span class="n">null_actions</span><span class="p">:</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span>
                        <span class="bp">self</span><span class="o">.</span><span class="n">sql_alter_column</span> <span class="o">%</span> <span class="p">{</span>
                            <span class="s2">&quot;table&quot;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">quote_name</span><span class="p">(</span><span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">db_table</span><span class="p">),</span>
                            <span class="s2">&quot;changes&quot;</span><span class="p">:</span> <span class="n">sql</span><span class="p">,</span>
                        <span class="p">},</span>
                        <span class="n">params</span><span class="p">,</span>
                    <span class="p">)</span>
        <span class="k">if</span> <span class="n">post_actions</span><span class="p">:</span>
            <span class="k">for</span> <span class="n">sql</span><span class="p">,</span> <span class="n">params</span> <span class="ow">in</span> <span class="n">post_actions</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="n">sql</span><span class="p">,</span> <span class="n">params</span><span class="p">)</span>
        <span class="c1"># If primary_key changed to False, delete the primary key constraint.</span>
        <span class="k">if</span> <span class="n">old_field</span><span class="o">.</span><span class="n">primary_key</span> <span class="ow">and</span> <span class="ow">not</span> <span class="n">new_field</span><span class="o">.</span><span class="n">primary_key</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_delete_primary_key</span><span class="p">(</span><span class="n">model</span><span class="p">,</span> <span class="n">strict</span><span class="p">)</span>
        <span class="c1"># Added a unique?</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_unique_should_be_added</span><span class="p">(</span><span class="n">old_field</span><span class="p">,</span> <span class="n">new_field</span><span class="p">):</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_create_unique_sql</span><span class="p">(</span><span class="n">model</span><span class="p">,</span> <span class="p">[</span><span class="n">new_field</span><span class="o">.</span><span class="n">column</span><span class="p">]))</span>
        <span class="c1"># Added an index? Add an index if db_index switched to True or a unique</span>
        <span class="c1"># constraint will no longer be used in lieu of an index. The following</span>
        <span class="c1"># lines from the truth table show all True cases; the rest are False:</span>
        <span class="c1">#</span>
        <span class="c1"># old_field.db_index | old_field.unique | new_field.db_index | new_field.unique</span>
        <span class="c1"># ------------------------------------------------------------------------------</span>
        <span class="c1"># False              | False            | True               | False</span>
        <span class="c1"># False              | True             | True               | False</span>
        <span class="c1"># True               | True             | True               | False</span>
        <span class="k">if</span> <span class="p">(</span><span class="ow">not</span> <span class="n">old_field</span><span class="o">.</span><span class="n">db_index</span> <span class="ow">or</span> <span class="n">old_field</span><span class="o">.</span><span class="n">unique</span><span class="p">)</span> <span class="ow">and</span> <span class="n">new_field</span><span class="o">.</span><span class="n">db_index</span> <span class="ow">and</span> <span class="ow">not</span> <span class="n">new_field</span><span class="o">.</span><span class="n">unique</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_create_index_sql</span><span class="p">(</span><span class="n">model</span><span class="p">,</span> <span class="p">[</span><span class="n">new_field</span><span class="p">]))</span>
        <span class="c1"># Type alteration on primary key? Then we need to alter the column</span>
        <span class="c1"># referring to us.</span>
        <span class="n">rels_to_update</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="k">if</span> <span class="n">old_field</span><span class="o">.</span><span class="n">primary_key</span> <span class="ow">and</span> <span class="n">new_field</span><span class="o">.</span><span class="n">primary_key</span> <span class="ow">and</span> <span class="n">old_type</span> <span class="o">!=</span> <span class="n">new_type</span><span class="p">:</span>
            <span class="n">rels_to_update</span><span class="o">.</span><span class="n">extend</span><span class="p">(</span><span class="n">_related_non_m2m_objects</span><span class="p">(</span><span class="n">old_field</span><span class="p">,</span> <span class="n">new_field</span><span class="p">))</span>
        <span class="c1"># Changed to become primary key?</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_field_became_primary_key</span><span class="p">(</span><span class="n">old_field</span><span class="p">,</span> <span class="n">new_field</span><span class="p">):</span>
            <span class="c1"># Make the new one</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_create_primary_key_sql</span><span class="p">(</span><span class="n">model</span><span class="p">,</span> <span class="n">new_field</span><span class="p">))</span>
            <span class="c1"># Update all referencing columns</span>
            <span class="n">rels_to_update</span><span class="o">.</span><span class="n">extend</span><span class="p">(</span><span class="n">_related_non_m2m_objects</span><span class="p">(</span><span class="n">old_field</span><span class="p">,</span> <span class="n">new_field</span><span class="p">))</span>
        <span class="c1"># Handle our type alters on the other end of rels from the PK stuff above</span>
        <span class="k">for</span> <span class="n">old_rel</span><span class="p">,</span> <span class="n">new_rel</span> <span class="ow">in</span> <span class="n">rels_to_update</span><span class="p">:</span>
            <span class="n">rel_db_params</span> <span class="o">=</span> <span class="n">new_rel</span><span class="o">.</span><span class="n">field</span><span class="o">.</span><span class="n">db_parameters</span><span class="p">(</span><span class="n">connection</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">connection</span><span class="p">)</span>
            <span class="n">rel_type</span> <span class="o">=</span> <span class="n">rel_db_params</span><span class="p">[</span><span class="s1">&#39;type&#39;</span><span class="p">]</span>
            <span class="n">fragment</span><span class="p">,</span> <span class="n">other_actions</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_alter_column_type_sql</span><span class="p">(</span>
                <span class="n">new_rel</span><span class="o">.</span><span class="n">related_model</span><span class="p">,</span> <span class="n">old_rel</span><span class="o">.</span><span class="n">field</span><span class="p">,</span> <span class="n">new_rel</span><span class="o">.</span><span class="n">field</span><span class="p">,</span> <span class="n">rel_type</span>
            <span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">sql_alter_column</span> <span class="o">%</span> <span class="p">{</span>
                    <span class="s2">&quot;table&quot;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">quote_name</span><span class="p">(</span><span class="n">new_rel</span><span class="o">.</span><span class="n">related_model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">db_table</span><span class="p">),</span>
                    <span class="s2">&quot;changes&quot;</span><span class="p">:</span> <span class="n">fragment</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span>
                <span class="p">},</span>
                <span class="n">fragment</span><span class="p">[</span><span class="mi">1</span><span class="p">],</span>
            <span class="p">)</span>
            <span class="k">for</span> <span class="n">sql</span><span class="p">,</span> <span class="n">params</span> <span class="ow">in</span> <span class="n">other_actions</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="n">sql</span><span class="p">,</span> <span class="n">params</span><span class="p">)</span>
        <span class="c1"># Does it have a foreign key?</span>
        <span class="k">if</span> <span class="p">(</span><span class="n">new_field</span><span class="o">.</span><span class="n">remote_field</span> <span class="ow">and</span>
                <span class="p">(</span><span class="n">fks_dropped</span> <span class="ow">or</span> <span class="ow">not</span> <span class="n">old_field</span><span class="o">.</span><span class="n">remote_field</span> <span class="ow">or</span> <span class="ow">not</span> <span class="n">old_field</span><span class="o">.</span><span class="n">db_constraint</span><span class="p">)</span> <span class="ow">and</span>
                <span class="n">new_field</span><span class="o">.</span><span class="n">db_constraint</span><span class="p">):</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_create_fk_sql</span><span class="p">(</span><span class="n">model</span><span class="p">,</span> <span class="n">new_field</span><span class="p">,</span> <span class="s2">&quot;_fk_</span><span class="si">%(to_table)s</span><span class="s2">_</span><span class="si">%(to_column)s</span><span class="s2">&quot;</span><span class="p">))</span>
        <span class="c1"># Rebuild FKs that pointed to us if we previously had to drop them</span>
        <span class="k">if</span> <span class="n">drop_foreign_keys</span><span class="p">:</span>
            <span class="k">for</span> <span class="n">rel</span> <span class="ow">in</span> <span class="n">new_field</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">related_objects</span><span class="p">:</span>
                <span class="k">if</span> <span class="n">_is_relevant_relation</span><span class="p">(</span><span class="n">rel</span><span class="p">,</span> <span class="n">new_field</span><span class="p">)</span> <span class="ow">and</span> <span class="n">rel</span><span class="o">.</span><span class="n">field</span><span class="o">.</span><span class="n">db_constraint</span><span class="p">:</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_create_fk_sql</span><span class="p">(</span><span class="n">rel</span><span class="o">.</span><span class="n">related_model</span><span class="p">,</span> <span class="n">rel</span><span class="o">.</span><span class="n">field</span><span class="p">,</span> <span class="s2">&quot;_fk&quot;</span><span class="p">))</span>
        <span class="c1"># Does it have check constraints we need to add?</span>
        <span class="k">if</span> <span class="n">old_db_params</span><span class="p">[</span><span class="s1">&#39;check&#39;</span><span class="p">]</span> <span class="o">!=</span> <span class="n">new_db_params</span><span class="p">[</span><span class="s1">&#39;check&#39;</span><span class="p">]</span> <span class="ow">and</span> <span class="n">new_db_params</span><span class="p">[</span><span class="s1">&#39;check&#39;</span><span class="p">]:</span>
            <span class="n">constraint_name</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_create_index_name</span><span class="p">(</span><span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">db_table</span><span class="p">,</span> <span class="p">[</span><span class="n">new_field</span><span class="o">.</span><span class="n">column</span><span class="p">],</span> <span class="n">suffix</span><span class="o">=</span><span class="s1">&#39;_check&#39;</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_create_check_sql</span><span class="p">(</span><span class="n">model</span><span class="p">,</span> <span class="n">constraint_name</span><span class="p">,</span> <span class="n">new_db_params</span><span class="p">[</span><span class="s1">&#39;check&#39;</span><span class="p">]))</span>
        <span class="c1"># Drop the default if we need to</span>
        <span class="c1"># (Django usually does not use in-database defaults)</span>
        <span class="k">if</span> <span class="n">needs_database_default</span><span class="p">:</span>
            <span class="n">changes_sql</span><span class="p">,</span> <span class="n">params</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_alter_column_default_sql</span><span class="p">(</span><span class="n">model</span><span class="p">,</span> <span class="n">old_field</span><span class="p">,</span> <span class="n">new_field</span><span class="p">,</span> <span class="n">drop</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
            <span class="n">sql</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">sql_alter_column</span> <span class="o">%</span> <span class="p">{</span>
                <span class="s2">&quot;table&quot;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">quote_name</span><span class="p">(</span><span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">db_table</span><span class="p">),</span>
                <span class="s2">&quot;changes&quot;</span><span class="p">:</span> <span class="n">changes_sql</span><span class="p">,</span>
            <span class="p">}</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="n">sql</span><span class="p">,</span> <span class="n">params</span><span class="p">)</span>
        <span class="c1"># Reset connection if required</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">connection</span><span class="o">.</span><span class="n">features</span><span class="o">.</span><span class="n">connection_persists_old_columns</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">connection</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>

    <span class="k">def</span> <span class="nf">_alter_column_null_sql</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">model</span><span class="p">,</span> <span class="n">old_field</span><span class="p">,</span> <span class="n">new_field</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Hook to specialize column null alteration.</span>

<span class="sd">        Return a (sql, params) fragment to set a column to null or non-null</span>
<span class="sd">        as required by new_field, or None if no changes are required.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">connection</span><span class="o">.</span><span class="n">features</span><span class="o">.</span><span class="n">interprets_empty_strings_as_nulls</span> <span class="ow">and</span>
                <span class="n">new_field</span><span class="o">.</span><span class="n">get_internal_type</span><span class="p">()</span> <span class="ow">in</span> <span class="p">(</span><span class="s2">&quot;CharField&quot;</span><span class="p">,</span> <span class="s2">&quot;TextField&quot;</span><span class="p">)):</span>
            <span class="c1"># The field is nullable in the database anyway, leave it alone.</span>
            <span class="k">return</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">new_db_params</span> <span class="o">=</span> <span class="n">new_field</span><span class="o">.</span><span class="n">db_parameters</span><span class="p">(</span><span class="n">connection</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">connection</span><span class="p">)</span>
            <span class="n">sql</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">sql_alter_column_null</span> <span class="k">if</span> <span class="n">new_field</span><span class="o">.</span><span class="n">null</span> <span class="k">else</span> <span class="bp">self</span><span class="o">.</span><span class="n">sql_alter_column_not_null</span>
            <span class="k">return</span> <span class="p">(</span>
                <span class="n">sql</span> <span class="o">%</span> <span class="p">{</span>
                    <span class="s1">&#39;column&#39;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">quote_name</span><span class="p">(</span><span class="n">new_field</span><span class="o">.</span><span class="n">column</span><span class="p">),</span>
                    <span class="s1">&#39;type&#39;</span><span class="p">:</span> <span class="n">new_db_params</span><span class="p">[</span><span class="s1">&#39;type&#39;</span><span class="p">],</span>
                <span class="p">},</span>
                <span class="p">[],</span>
            <span class="p">)</span>

    <span class="k">def</span> <span class="nf">_alter_column_default_sql</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">model</span><span class="p">,</span> <span class="n">old_field</span><span class="p">,</span> <span class="n">new_field</span><span class="p">,</span> <span class="n">drop</span><span class="o">=</span><span class="kc">False</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Hook to specialize column default alteration.</span>

<span class="sd">        Return a (sql, params) fragment to add or drop (depending on the drop</span>
<span class="sd">        argument) a default to new_field&#39;s column.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">new_default</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">effective_default</span><span class="p">(</span><span class="n">new_field</span><span class="p">)</span>
        <span class="n">default</span> <span class="o">=</span> <span class="s1">&#39;</span><span class="si">%s</span><span class="s1">&#39;</span>
        <span class="n">params</span> <span class="o">=</span> <span class="p">[</span><span class="n">new_default</span><span class="p">]</span>

        <span class="k">if</span> <span class="n">drop</span><span class="p">:</span>
            <span class="n">params</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="k">elif</span> <span class="bp">self</span><span class="o">.</span><span class="n">connection</span><span class="o">.</span><span class="n">features</span><span class="o">.</span><span class="n">requires_literal_defaults</span><span class="p">:</span>
            <span class="c1"># Some databases (Oracle) can&#39;t take defaults as a parameter</span>
            <span class="c1"># If this is the case, the SchemaEditor for that database should</span>
            <span class="c1"># implement prepare_default().</span>
            <span class="n">default</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">prepare_default</span><span class="p">(</span><span class="n">new_default</span><span class="p">)</span>
            <span class="n">params</span> <span class="o">=</span> <span class="p">[]</span>

        <span class="n">new_db_params</span> <span class="o">=</span> <span class="n">new_field</span><span class="o">.</span><span class="n">db_parameters</span><span class="p">(</span><span class="n">connection</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">connection</span><span class="p">)</span>
        <span class="n">sql</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">sql_alter_column_no_default</span> <span class="k">if</span> <span class="n">drop</span> <span class="k">else</span> <span class="bp">self</span><span class="o">.</span><span class="n">sql_alter_column_default</span>
        <span class="k">return</span> <span class="p">(</span>
            <span class="n">sql</span> <span class="o">%</span> <span class="p">{</span>
                <span class="s1">&#39;column&#39;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">quote_name</span><span class="p">(</span><span class="n">new_field</span><span class="o">.</span><span class="n">column</span><span class="p">),</span>
                <span class="s1">&#39;type&#39;</span><span class="p">:</span> <span class="n">new_db_params</span><span class="p">[</span><span class="s1">&#39;type&#39;</span><span class="p">],</span>
                <span class="s1">&#39;default&#39;</span><span class="p">:</span> <span class="n">default</span><span class="p">,</span>
            <span class="p">},</span>
            <span class="n">params</span><span class="p">,</span>
        <span class="p">)</span>

    <span class="k">def</span> <span class="nf">_alter_column_type_sql</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">model</span><span class="p">,</span> <span class="n">old_field</span><span class="p">,</span> <span class="n">new_field</span><span class="p">,</span> <span class="n">new_type</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Hook to specialize column type alteration for different backends,</span>
<span class="sd">        for cases when a creation type is different to an alteration type</span>
<span class="sd">        (e.g. SERIAL in PostgreSQL, PostGIS fields).</span>

<span class="sd">        Return a two-tuple of: an SQL fragment of (sql, params) to insert into</span>
<span class="sd">        an ALTER TABLE statement and a list of extra (sql, params) tuples to</span>
<span class="sd">        run once the field is altered.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="p">(</span>
            <span class="p">(</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">sql_alter_column_type</span> <span class="o">%</span> <span class="p">{</span>
                    <span class="s2">&quot;column&quot;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">quote_name</span><span class="p">(</span><span class="n">new_field</span><span class="o">.</span><span class="n">column</span><span class="p">),</span>
                    <span class="s2">&quot;type&quot;</span><span class="p">:</span> <span class="n">new_type</span><span class="p">,</span>
                <span class="p">},</span>
                <span class="p">[],</span>
            <span class="p">),</span>
            <span class="p">[],</span>
        <span class="p">)</span>

    <span class="k">def</span> <span class="nf">_alter_many_to_many</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">model</span><span class="p">,</span> <span class="n">old_field</span><span class="p">,</span> <span class="n">new_field</span><span class="p">,</span> <span class="n">strict</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Alter M2Ms to repoint their to= endpoints.&quot;&quot;&quot;</span>
        <span class="c1"># Rename the through table</span>
        <span class="k">if</span> <span class="n">old_field</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">through</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">db_table</span> <span class="o">!=</span> <span class="n">new_field</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">through</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">db_table</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">alter_db_table</span><span class="p">(</span><span class="n">old_field</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">through</span><span class="p">,</span> <span class="n">old_field</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">through</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">db_table</span><span class="p">,</span>
                                <span class="n">new_field</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">through</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">db_table</span><span class="p">)</span>
        <span class="c1"># Repoint the FK to the other side</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">alter_field</span><span class="p">(</span>
            <span class="n">new_field</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">through</span><span class="p">,</span>
            <span class="c1"># We need the field that points to the target model, so we can tell alter_field to change it -</span>
            <span class="c1"># this is m2m_reverse_field_name() (as opposed to m2m_field_name, which points to our model)</span>
            <span class="n">old_field</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">through</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">get_field</span><span class="p">(</span><span class="n">old_field</span><span class="o">.</span><span class="n">m2m_reverse_field_name</span><span class="p">()),</span>
            <span class="n">new_field</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">through</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">get_field</span><span class="p">(</span><span class="n">new_field</span><span class="o">.</span><span class="n">m2m_reverse_field_name</span><span class="p">()),</span>
        <span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">alter_field</span><span class="p">(</span>
            <span class="n">new_field</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">through</span><span class="p">,</span>
            <span class="c1"># for self-referential models we need to alter field from the other end too</span>
            <span class="n">old_field</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">through</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">get_field</span><span class="p">(</span><span class="n">old_field</span><span class="o">.</span><span class="n">m2m_field_name</span><span class="p">()),</span>
            <span class="n">new_field</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">through</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">get_field</span><span class="p">(</span><span class="n">new_field</span><span class="o">.</span><span class="n">m2m_field_name</span><span class="p">()),</span>
        <span class="p">)</span>

    <span class="k">def</span> <span class="nf">_create_index_name</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">table_name</span><span class="p">,</span> <span class="n">column_names</span><span class="p">,</span> <span class="n">suffix</span><span class="o">=</span><span class="s2">&quot;&quot;</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Generate a unique name for an index/unique constraint.</span>

<span class="sd">        The name is divided into 3 parts: the table name, the column names,</span>
<span class="sd">        and a unique digest and suffix.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">_</span><span class="p">,</span> <span class="n">table_name</span> <span class="o">=</span> <span class="n">split_identifier</span><span class="p">(</span><span class="n">table_name</span><span class="p">)</span>
        <span class="n">hash_suffix_part</span> <span class="o">=</span> <span class="s1">&#39;</span><span class="si">%s%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">names_digest</span><span class="p">(</span><span class="n">table_name</span><span class="p">,</span> <span class="o">*</span><span class="n">column_names</span><span class="p">,</span> <span class="n">length</span><span class="o">=</span><span class="mi">8</span><span class="p">),</span> <span class="n">suffix</span><span class="p">)</span>
        <span class="n">max_length</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">connection</span><span class="o">.</span><span class="n">ops</span><span class="o">.</span><span class="n">max_name_length</span><span class="p">()</span> <span class="ow">or</span> <span class="mi">200</span>
        <span class="c1"># If everything fits into max_length, use that name.</span>
        <span class="n">index_name</span> <span class="o">=</span> <span class="s1">&#39;</span><span class="si">%s</span><span class="s1">_</span><span class="si">%s</span><span class="s1">_</span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">table_name</span><span class="p">,</span> <span class="s1">&#39;_&#39;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">column_names</span><span class="p">),</span> <span class="n">hash_suffix_part</span><span class="p">)</span>
        <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">index_name</span><span class="p">)</span> <span class="o">&lt;=</span> <span class="n">max_length</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">index_name</span>
        <span class="c1"># Shorten a long suffix.</span>
        <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">hash_suffix_part</span><span class="p">)</span> <span class="o">&gt;</span> <span class="n">max_length</span> <span class="o">/</span> <span class="mi">3</span><span class="p">:</span>
            <span class="n">hash_suffix_part</span> <span class="o">=</span> <span class="n">hash_suffix_part</span><span class="p">[:</span><span class="n">max_length</span> <span class="o">//</span> <span class="mi">3</span><span class="p">]</span>
        <span class="n">other_length</span> <span class="o">=</span> <span class="p">(</span><span class="n">max_length</span> <span class="o">-</span> <span class="nb">len</span><span class="p">(</span><span class="n">hash_suffix_part</span><span class="p">))</span> <span class="o">//</span> <span class="mi">2</span> <span class="o">-</span> <span class="mi">1</span>
        <span class="n">index_name</span> <span class="o">=</span> <span class="s1">&#39;</span><span class="si">%s</span><span class="s1">_</span><span class="si">%s</span><span class="s1">_</span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="p">(</span>
            <span class="n">table_name</span><span class="p">[:</span><span class="n">other_length</span><span class="p">],</span>
            <span class="s1">&#39;_&#39;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">column_names</span><span class="p">)[:</span><span class="n">other_length</span><span class="p">],</span>
            <span class="n">hash_suffix_part</span><span class="p">,</span>
        <span class="p">)</span>
        <span class="c1"># Prepend D if needed to prevent the name from starting with an</span>
        <span class="c1"># underscore or a number (not permitted on Oracle).</span>
        <span class="k">if</span> <span class="n">index_name</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">==</span> <span class="s2">&quot;_&quot;</span> <span class="ow">or</span> <span class="n">index_name</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">isdigit</span><span class="p">():</span>
            <span class="n">index_name</span> <span class="o">=</span> <span class="s2">&quot;D</span><span class="si">%s</span><span class="s2">&quot;</span> <span class="o">%</span> <span class="n">index_name</span><span class="p">[:</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span>
        <span class="k">return</span> <span class="n">index_name</span>

    <span class="k">def</span> <span class="nf">_get_index_tablespace_sql</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">model</span><span class="p">,</span> <span class="n">fields</span><span class="p">,</span> <span class="n">db_tablespace</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="k">if</span> <span class="n">db_tablespace</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">fields</span><span class="p">)</span> <span class="o">==</span> <span class="mi">1</span> <span class="ow">and</span> <span class="n">fields</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">db_tablespace</span><span class="p">:</span>
                <span class="n">db_tablespace</span> <span class="o">=</span> <span class="n">fields</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">db_tablespace</span>
            <span class="k">elif</span> <span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">db_tablespace</span><span class="p">:</span>
                <span class="n">db_tablespace</span> <span class="o">=</span> <span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">db_tablespace</span>
        <span class="k">if</span> <span class="n">db_tablespace</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="k">return</span> <span class="s1">&#39; &#39;</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">connection</span><span class="o">.</span><span class="n">ops</span><span class="o">.</span><span class="n">tablespace_sql</span><span class="p">(</span><span class="n">db_tablespace</span><span class="p">)</span>
        <span class="k">return</span> <span class="s1">&#39;&#39;</span>

    <span class="k">def</span> <span class="nf">_create_index_sql</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">model</span><span class="p">,</span> <span class="n">fields</span><span class="p">,</span> <span class="o">*</span><span class="p">,</span> <span class="n">name</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">suffix</span><span class="o">=</span><span class="s1">&#39;&#39;</span><span class="p">,</span> <span class="n">using</span><span class="o">=</span><span class="s1">&#39;&#39;</span><span class="p">,</span>
                          <span class="n">db_tablespace</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">col_suffixes</span><span class="o">=</span><span class="p">(),</span> <span class="n">sql</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">opclasses</span><span class="o">=</span><span class="p">(),</span>
                          <span class="n">condition</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Return the SQL statement to create the index for one or several fields.</span>
<span class="sd">        `sql` can be specified if the syntax differs from the standard (GIS</span>
<span class="sd">        indexes, ...).</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">tablespace_sql</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_get_index_tablespace_sql</span><span class="p">(</span><span class="n">model</span><span class="p">,</span> <span class="n">fields</span><span class="p">,</span> <span class="n">db_tablespace</span><span class="o">=</span><span class="n">db_tablespace</span><span class="p">)</span>
        <span class="n">columns</span> <span class="o">=</span> <span class="p">[</span><span class="n">field</span><span class="o">.</span><span class="n">column</span> <span class="k">for</span> <span class="n">field</span> <span class="ow">in</span> <span class="n">fields</span><span class="p">]</span>
        <span class="n">sql_create_index</span> <span class="o">=</span> <span class="n">sql</span> <span class="ow">or</span> <span class="bp">self</span><span class="o">.</span><span class="n">sql_create_index</span>
        <span class="n">table</span> <span class="o">=</span> <span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">db_table</span>

        <span class="k">def</span> <span class="nf">create_index_name</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
            <span class="k">nonlocal</span> <span class="n">name</span>
            <span class="k">if</span> <span class="n">name</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
                <span class="n">name</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_create_index_name</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
            <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">quote_name</span><span class="p">(</span><span class="n">name</span><span class="p">)</span>

        <span class="k">return</span> <span class="n">Statement</span><span class="p">(</span>
            <span class="n">sql_create_index</span><span class="p">,</span>
            <span class="n">table</span><span class="o">=</span><span class="n">Table</span><span class="p">(</span><span class="n">table</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">quote_name</span><span class="p">),</span>
            <span class="n">name</span><span class="o">=</span><span class="n">IndexName</span><span class="p">(</span><span class="n">table</span><span class="p">,</span> <span class="n">columns</span><span class="p">,</span> <span class="n">suffix</span><span class="p">,</span> <span class="n">create_index_name</span><span class="p">),</span>
            <span class="n">using</span><span class="o">=</span><span class="n">using</span><span class="p">,</span>
            <span class="n">columns</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">_index_columns</span><span class="p">(</span><span class="n">table</span><span class="p">,</span> <span class="n">columns</span><span class="p">,</span> <span class="n">col_suffixes</span><span class="p">,</span> <span class="n">opclasses</span><span class="p">),</span>
            <span class="n">extra</span><span class="o">=</span><span class="n">tablespace_sql</span><span class="p">,</span>
            <span class="n">condition</span><span class="o">=</span><span class="p">(</span><span class="s1">&#39; WHERE &#39;</span> <span class="o">+</span> <span class="n">condition</span><span class="p">)</span> <span class="k">if</span> <span class="n">condition</span> <span class="k">else</span> <span class="s1">&#39;&#39;</span><span class="p">,</span>
        <span class="p">)</span>

    <span class="k">def</span> <span class="nf">_delete_index_sql</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">model</span><span class="p">,</span> <span class="n">name</span><span class="p">):</span>
        <span class="k">return</span> <span class="n">Statement</span><span class="p">(</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">sql_delete_index</span><span class="p">,</span>
            <span class="n">table</span><span class="o">=</span><span class="n">Table</span><span class="p">(</span><span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">db_table</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">quote_name</span><span class="p">),</span>
            <span class="n">name</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">quote_name</span><span class="p">(</span><span class="n">name</span><span class="p">),</span>
        <span class="p">)</span>

    <span class="k">def</span> <span class="nf">_index_columns</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">table</span><span class="p">,</span> <span class="n">columns</span><span class="p">,</span> <span class="n">col_suffixes</span><span class="p">,</span> <span class="n">opclasses</span><span class="p">):</span>
        <span class="k">return</span> <span class="n">Columns</span><span class="p">(</span><span class="n">table</span><span class="p">,</span> <span class="n">columns</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">quote_name</span><span class="p">,</span> <span class="n">col_suffixes</span><span class="o">=</span><span class="n">col_suffixes</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">_model_indexes_sql</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">model</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Return a list of all index SQL statements (field indexes,</span>
<span class="sd">        index_together, Meta.indexes) for the specified model.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">managed</span> <span class="ow">or</span> <span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">proxy</span> <span class="ow">or</span> <span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">swapped</span><span class="p">:</span>
            <span class="k">return</span> <span class="p">[]</span>
        <span class="n">output</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="k">for</span> <span class="n">field</span> <span class="ow">in</span> <span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">local_fields</span><span class="p">:</span>
            <span class="n">output</span><span class="o">.</span><span class="n">extend</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_field_indexes_sql</span><span class="p">(</span><span class="n">model</span><span class="p">,</span> <span class="n">field</span><span class="p">))</span>

        <span class="k">for</span> <span class="n">field_names</span> <span class="ow">in</span> <span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">index_together</span><span class="p">:</span>
            <span class="n">fields</span> <span class="o">=</span> <span class="p">[</span><span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">get_field</span><span class="p">(</span><span class="n">field</span><span class="p">)</span> <span class="k">for</span> <span class="n">field</span> <span class="ow">in</span> <span class="n">field_names</span><span class="p">]</span>
            <span class="n">output</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_create_index_sql</span><span class="p">(</span><span class="n">model</span><span class="p">,</span> <span class="n">fields</span><span class="p">,</span> <span class="n">suffix</span><span class="o">=</span><span class="s2">&quot;_idx&quot;</span><span class="p">))</span>

        <span class="k">for</span> <span class="n">index</span> <span class="ow">in</span> <span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">indexes</span><span class="p">:</span>
            <span class="n">output</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">index</span><span class="o">.</span><span class="n">create_sql</span><span class="p">(</span><span class="n">model</span><span class="p">,</span> <span class="bp">self</span><span class="p">))</span>
        <span class="k">return</span> <span class="n">output</span>

    <span class="k">def</span> <span class="nf">_field_indexes_sql</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">model</span><span class="p">,</span> <span class="n">field</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Return a list of all index SQL statements for the specified field.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">output</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_field_should_be_indexed</span><span class="p">(</span><span class="n">model</span><span class="p">,</span> <span class="n">field</span><span class="p">):</span>
            <span class="n">output</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_create_index_sql</span><span class="p">(</span><span class="n">model</span><span class="p">,</span> <span class="p">[</span><span class="n">field</span><span class="p">]))</span>
        <span class="k">return</span> <span class="n">output</span>

    <span class="k">def</span> <span class="nf">_field_should_be_indexed</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">model</span><span class="p">,</span> <span class="n">field</span><span class="p">):</span>
        <span class="k">return</span> <span class="n">field</span><span class="o">.</span><span class="n">db_index</span> <span class="ow">and</span> <span class="ow">not</span> <span class="n">field</span><span class="o">.</span><span class="n">unique</span>

    <span class="k">def</span> <span class="nf">_field_became_primary_key</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">old_field</span><span class="p">,</span> <span class="n">new_field</span><span class="p">):</span>
        <span class="k">return</span> <span class="ow">not</span> <span class="n">old_field</span><span class="o">.</span><span class="n">primary_key</span> <span class="ow">and</span> <span class="n">new_field</span><span class="o">.</span><span class="n">primary_key</span>

    <span class="k">def</span> <span class="nf">_unique_should_be_added</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">old_field</span><span class="p">,</span> <span class="n">new_field</span><span class="p">):</span>
        <span class="k">return</span> <span class="p">(</span><span class="ow">not</span> <span class="n">old_field</span><span class="o">.</span><span class="n">unique</span> <span class="ow">and</span> <span class="n">new_field</span><span class="o">.</span><span class="n">unique</span><span class="p">)</span> <span class="ow">or</span> <span class="p">(</span>
            <span class="n">old_field</span><span class="o">.</span><span class="n">primary_key</span> <span class="ow">and</span> <span class="ow">not</span> <span class="n">new_field</span><span class="o">.</span><span class="n">primary_key</span> <span class="ow">and</span> <span class="n">new_field</span><span class="o">.</span><span class="n">unique</span>
        <span class="p">)</span>

    <span class="k">def</span> <span class="nf">_rename_field_sql</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">table</span><span class="p">,</span> <span class="n">old_field</span><span class="p">,</span> <span class="n">new_field</span><span class="p">,</span> <span class="n">new_type</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">sql_rename_column</span> <span class="o">%</span> <span class="p">{</span>
            <span class="s2">&quot;table&quot;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">quote_name</span><span class="p">(</span><span class="n">table</span><span class="p">),</span>
            <span class="s2">&quot;old_column&quot;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">quote_name</span><span class="p">(</span><span class="n">old_field</span><span class="o">.</span><span class="n">column</span><span class="p">),</span>
            <span class="s2">&quot;new_column&quot;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">quote_name</span><span class="p">(</span><span class="n">new_field</span><span class="o">.</span><span class="n">column</span><span class="p">),</span>
            <span class="s2">&quot;type&quot;</span><span class="p">:</span> <span class="n">new_type</span><span class="p">,</span>
        <span class="p">}</span>

    <span class="k">def</span> <span class="nf">_create_fk_sql</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">model</span><span class="p">,</span> <span class="n">field</span><span class="p">,</span> <span class="n">suffix</span><span class="p">):</span>
        <span class="k">def</span> <span class="nf">create_fk_name</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
            <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">quote_name</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_create_index_name</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">))</span>

        <span class="n">table</span> <span class="o">=</span> <span class="n">Table</span><span class="p">(</span><span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">db_table</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">quote_name</span><span class="p">)</span>
        <span class="n">name</span> <span class="o">=</span> <span class="n">ForeignKeyName</span><span class="p">(</span>
            <span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">db_table</span><span class="p">,</span>
            <span class="p">[</span><span class="n">field</span><span class="o">.</span><span class="n">column</span><span class="p">],</span>
            <span class="n">split_identifier</span><span class="p">(</span><span class="n">field</span><span class="o">.</span><span class="n">target_field</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">db_table</span><span class="p">)[</span><span class="mi">1</span><span class="p">],</span>
            <span class="p">[</span><span class="n">field</span><span class="o">.</span><span class="n">target_field</span><span class="o">.</span><span class="n">column</span><span class="p">],</span>
            <span class="n">suffix</span><span class="p">,</span>
            <span class="n">create_fk_name</span><span class="p">,</span>
        <span class="p">)</span>
        <span class="n">column</span> <span class="o">=</span> <span class="n">Columns</span><span class="p">(</span><span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">db_table</span><span class="p">,</span> <span class="p">[</span><span class="n">field</span><span class="o">.</span><span class="n">column</span><span class="p">],</span> <span class="bp">self</span><span class="o">.</span><span class="n">quote_name</span><span class="p">)</span>
        <span class="n">to_table</span> <span class="o">=</span> <span class="n">Table</span><span class="p">(</span><span class="n">field</span><span class="o">.</span><span class="n">target_field</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">db_table</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">quote_name</span><span class="p">)</span>
        <span class="n">to_column</span> <span class="o">=</span> <span class="n">Columns</span><span class="p">(</span><span class="n">field</span><span class="o">.</span><span class="n">target_field</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">db_table</span><span class="p">,</span> <span class="p">[</span><span class="n">field</span><span class="o">.</span><span class="n">target_field</span><span class="o">.</span><span class="n">column</span><span class="p">],</span> <span class="bp">self</span><span class="o">.</span><span class="n">quote_name</span><span class="p">)</span>
        <span class="n">deferrable</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">connection</span><span class="o">.</span><span class="n">ops</span><span class="o">.</span><span class="n">deferrable_sql</span><span class="p">()</span>
        <span class="k">return</span> <span class="n">Statement</span><span class="p">(</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">sql_create_fk</span><span class="p">,</span>
            <span class="n">table</span><span class="o">=</span><span class="n">table</span><span class="p">,</span>
            <span class="n">name</span><span class="o">=</span><span class="n">name</span><span class="p">,</span>
            <span class="n">column</span><span class="o">=</span><span class="n">column</span><span class="p">,</span>
            <span class="n">to_table</span><span class="o">=</span><span class="n">to_table</span><span class="p">,</span>
            <span class="n">to_column</span><span class="o">=</span><span class="n">to_column</span><span class="p">,</span>
            <span class="n">deferrable</span><span class="o">=</span><span class="n">deferrable</span><span class="p">,</span>
        <span class="p">)</span>

    <span class="k">def</span> <span class="nf">_delete_fk_sql</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">model</span><span class="p">,</span> <span class="n">name</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_delete_constraint_sql</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">sql_delete_fk</span><span class="p">,</span> <span class="n">model</span><span class="p">,</span> <span class="n">name</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">_unique_sql</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">model</span><span class="p">,</span> <span class="n">fields</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="n">condition</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="k">if</span> <span class="n">condition</span><span class="p">:</span>
            <span class="c1"># Databases support conditional unique constraints via a unique</span>
            <span class="c1"># index.</span>
            <span class="n">sql</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_create_unique_sql</span><span class="p">(</span><span class="n">model</span><span class="p">,</span> <span class="n">fields</span><span class="p">,</span> <span class="n">name</span><span class="o">=</span><span class="n">name</span><span class="p">,</span> <span class="n">condition</span><span class="o">=</span><span class="n">condition</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">sql</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">deferred_sql</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">sql</span><span class="p">)</span>
            <span class="k">return</span> <span class="kc">None</span>
        <span class="n">constraint</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">sql_unique_constraint</span> <span class="o">%</span> <span class="p">{</span>
            <span class="s1">&#39;columns&#39;</span><span class="p">:</span> <span class="s1">&#39;, &#39;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="nb">map</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">quote_name</span><span class="p">,</span> <span class="n">fields</span><span class="p">)),</span>
        <span class="p">}</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">sql_constraint</span> <span class="o">%</span> <span class="p">{</span>
            <span class="s1">&#39;name&#39;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">quote_name</span><span class="p">(</span><span class="n">name</span><span class="p">),</span>
            <span class="s1">&#39;constraint&#39;</span><span class="p">:</span> <span class="n">constraint</span><span class="p">,</span>
        <span class="p">}</span>

    <span class="k">def</span> <span class="nf">_create_unique_sql</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">model</span><span class="p">,</span> <span class="n">columns</span><span class="p">,</span> <span class="n">name</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">condition</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="k">def</span> <span class="nf">create_unique_name</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
            <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">quote_name</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_create_index_name</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">))</span>

        <span class="n">table</span> <span class="o">=</span> <span class="n">Table</span><span class="p">(</span><span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">db_table</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">quote_name</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">name</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">name</span> <span class="o">=</span> <span class="n">IndexName</span><span class="p">(</span><span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">db_table</span><span class="p">,</span> <span class="n">columns</span><span class="p">,</span> <span class="s1">&#39;_uniq&#39;</span><span class="p">,</span> <span class="n">create_unique_name</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">name</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">quote_name</span><span class="p">(</span><span class="n">name</span><span class="p">)</span>
        <span class="n">columns</span> <span class="o">=</span> <span class="n">Columns</span><span class="p">(</span><span class="n">table</span><span class="p">,</span> <span class="n">columns</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">quote_name</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">condition</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">Statement</span><span class="p">(</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">sql_create_unique_index</span><span class="p">,</span>
                <span class="n">table</span><span class="o">=</span><span class="n">table</span><span class="p">,</span>
                <span class="n">name</span><span class="o">=</span><span class="n">name</span><span class="p">,</span>
                <span class="n">columns</span><span class="o">=</span><span class="n">columns</span><span class="p">,</span>
                <span class="n">condition</span><span class="o">=</span><span class="s1">&#39; WHERE &#39;</span> <span class="o">+</span> <span class="n">condition</span><span class="p">,</span>
            <span class="p">)</span> <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">connection</span><span class="o">.</span><span class="n">features</span><span class="o">.</span><span class="n">supports_partial_indexes</span> <span class="k">else</span> <span class="kc">None</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">Statement</span><span class="p">(</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">sql_create_unique</span><span class="p">,</span>
                <span class="n">table</span><span class="o">=</span><span class="n">table</span><span class="p">,</span>
                <span class="n">name</span><span class="o">=</span><span class="n">name</span><span class="p">,</span>
                <span class="n">columns</span><span class="o">=</span><span class="n">columns</span><span class="p">,</span>
            <span class="p">)</span>

    <span class="k">def</span> <span class="nf">_delete_unique_sql</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">model</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="n">condition</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="k">if</span> <span class="n">condition</span><span class="p">:</span>
            <span class="k">return</span> <span class="p">(</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">_delete_constraint_sql</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">sql_delete_index</span><span class="p">,</span> <span class="n">model</span><span class="p">,</span> <span class="n">name</span><span class="p">)</span>
                <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">connection</span><span class="o">.</span><span class="n">features</span><span class="o">.</span><span class="n">supports_partial_indexes</span> <span class="k">else</span> <span class="kc">None</span>
            <span class="p">)</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_delete_constraint_sql</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">sql_delete_unique</span><span class="p">,</span> <span class="n">model</span><span class="p">,</span> <span class="n">name</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">_check_sql</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="n">check</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">sql_constraint</span> <span class="o">%</span> <span class="p">{</span>
            <span class="s1">&#39;name&#39;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">quote_name</span><span class="p">(</span><span class="n">name</span><span class="p">),</span>
            <span class="s1">&#39;constraint&#39;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">sql_check_constraint</span> <span class="o">%</span> <span class="p">{</span><span class="s1">&#39;check&#39;</span><span class="p">:</span> <span class="n">check</span><span class="p">},</span>
        <span class="p">}</span>

    <span class="k">def</span> <span class="nf">_create_check_sql</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">model</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="n">check</span><span class="p">):</span>
        <span class="k">return</span> <span class="n">Statement</span><span class="p">(</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">sql_create_check</span><span class="p">,</span>
            <span class="n">table</span><span class="o">=</span><span class="n">Table</span><span class="p">(</span><span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">db_table</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">quote_name</span><span class="p">),</span>
            <span class="n">name</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">quote_name</span><span class="p">(</span><span class="n">name</span><span class="p">),</span>
            <span class="n">check</span><span class="o">=</span><span class="n">check</span><span class="p">,</span>
        <span class="p">)</span>

    <span class="k">def</span> <span class="nf">_delete_check_sql</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">model</span><span class="p">,</span> <span class="n">name</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_delete_constraint_sql</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">sql_delete_check</span><span class="p">,</span> <span class="n">model</span><span class="p">,</span> <span class="n">name</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">_delete_constraint_sql</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">template</span><span class="p">,</span> <span class="n">model</span><span class="p">,</span> <span class="n">name</span><span class="p">):</span>
        <span class="k">return</span> <span class="n">Statement</span><span class="p">(</span>
            <span class="n">template</span><span class="p">,</span>
            <span class="n">table</span><span class="o">=</span><span class="n">Table</span><span class="p">(</span><span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">db_table</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">quote_name</span><span class="p">),</span>
            <span class="n">name</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">quote_name</span><span class="p">(</span><span class="n">name</span><span class="p">),</span>
        <span class="p">)</span>

    <span class="k">def</span> <span class="nf">_constraint_names</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">model</span><span class="p">,</span> <span class="n">column_names</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">unique</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
                          <span class="n">primary_key</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">index</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">foreign_key</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
                          <span class="n">check</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">type_</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">exclude</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Return all constraint names matching the columns and conditions.&quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="n">column_names</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">column_names</span> <span class="o">=</span> <span class="p">[</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">connection</span><span class="o">.</span><span class="n">introspection</span><span class="o">.</span><span class="n">identifier_converter</span><span class="p">(</span><span class="n">name</span><span class="p">)</span>
                <span class="k">for</span> <span class="n">name</span> <span class="ow">in</span> <span class="n">column_names</span>
            <span class="p">]</span>
        <span class="k">with</span> <span class="bp">self</span><span class="o">.</span><span class="n">connection</span><span class="o">.</span><span class="n">cursor</span><span class="p">()</span> <span class="k">as</span> <span class="n">cursor</span><span class="p">:</span>
            <span class="n">constraints</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">connection</span><span class="o">.</span><span class="n">introspection</span><span class="o">.</span><span class="n">get_constraints</span><span class="p">(</span><span class="n">cursor</span><span class="p">,</span> <span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">db_table</span><span class="p">)</span>
        <span class="n">result</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="k">for</span> <span class="n">name</span><span class="p">,</span> <span class="n">infodict</span> <span class="ow">in</span> <span class="n">constraints</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
            <span class="k">if</span> <span class="n">column_names</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">or</span> <span class="n">column_names</span> <span class="o">==</span> <span class="n">infodict</span><span class="p">[</span><span class="s1">&#39;columns&#39;</span><span class="p">]:</span>
                <span class="k">if</span> <span class="n">unique</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">infodict</span><span class="p">[</span><span class="s1">&#39;unique&#39;</span><span class="p">]</span> <span class="o">!=</span> <span class="n">unique</span><span class="p">:</span>
                    <span class="k">continue</span>
                <span class="k">if</span> <span class="n">primary_key</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">infodict</span><span class="p">[</span><span class="s1">&#39;primary_key&#39;</span><span class="p">]</span> <span class="o">!=</span> <span class="n">primary_key</span><span class="p">:</span>
                    <span class="k">continue</span>
                <span class="k">if</span> <span class="n">index</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">infodict</span><span class="p">[</span><span class="s1">&#39;index&#39;</span><span class="p">]</span> <span class="o">!=</span> <span class="n">index</span><span class="p">:</span>
                    <span class="k">continue</span>
                <span class="k">if</span> <span class="n">check</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">infodict</span><span class="p">[</span><span class="s1">&#39;check&#39;</span><span class="p">]</span> <span class="o">!=</span> <span class="n">check</span><span class="p">:</span>
                    <span class="k">continue</span>
                <span class="k">if</span> <span class="n">foreign_key</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span> <span class="ow">and</span> <span class="ow">not</span> <span class="n">infodict</span><span class="p">[</span><span class="s1">&#39;foreign_key&#39;</span><span class="p">]:</span>
                    <span class="k">continue</span>
                <span class="k">if</span> <span class="n">type_</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">infodict</span><span class="p">[</span><span class="s1">&#39;type&#39;</span><span class="p">]</span> <span class="o">!=</span> <span class="n">type_</span><span class="p">:</span>
                    <span class="k">continue</span>
                <span class="k">if</span> <span class="ow">not</span> <span class="n">exclude</span> <span class="ow">or</span> <span class="n">name</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">exclude</span><span class="p">:</span>
                    <span class="n">result</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">name</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">result</span>

    <span class="k">def</span> <span class="nf">_delete_primary_key</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">model</span><span class="p">,</span> <span class="n">strict</span><span class="o">=</span><span class="kc">False</span><span class="p">):</span>
        <span class="n">constraint_names</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_constraint_names</span><span class="p">(</span><span class="n">model</span><span class="p">,</span> <span class="n">primary_key</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">strict</span> <span class="ow">and</span> <span class="nb">len</span><span class="p">(</span><span class="n">constraint_names</span><span class="p">)</span> <span class="o">!=</span> <span class="mi">1</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s1">&#39;Found wrong number (</span><span class="si">%s</span><span class="s1">) of PK constraints for </span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="p">(</span>
                <span class="nb">len</span><span class="p">(</span><span class="n">constraint_names</span><span class="p">),</span>
                <span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">db_table</span><span class="p">,</span>
            <span class="p">))</span>
        <span class="k">for</span> <span class="n">constraint_name</span> <span class="ow">in</span> <span class="n">constraint_names</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_delete_primary_key_sql</span><span class="p">(</span><span class="n">model</span><span class="p">,</span> <span class="n">constraint_name</span><span class="p">))</span>

    <span class="k">def</span> <span class="nf">_create_primary_key_sql</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">model</span><span class="p">,</span> <span class="n">field</span><span class="p">):</span>
        <span class="k">return</span> <span class="n">Statement</span><span class="p">(</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">sql_create_pk</span><span class="p">,</span>
            <span class="n">table</span><span class="o">=</span><span class="n">Table</span><span class="p">(</span><span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">db_table</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">quote_name</span><span class="p">),</span>
            <span class="n">name</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">quote_name</span><span class="p">(</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">_create_index_name</span><span class="p">(</span><span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">db_table</span><span class="p">,</span> <span class="p">[</span><span class="n">field</span><span class="o">.</span><span class="n">column</span><span class="p">],</span> <span class="n">suffix</span><span class="o">=</span><span class="s2">&quot;_pk&quot;</span><span class="p">)</span>
            <span class="p">),</span>
            <span class="n">columns</span><span class="o">=</span><span class="n">Columns</span><span class="p">(</span><span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">db_table</span><span class="p">,</span> <span class="p">[</span><span class="n">field</span><span class="o">.</span><span class="n">column</span><span class="p">],</span> <span class="bp">self</span><span class="o">.</span><span class="n">quote_name</span><span class="p">),</span>
        <span class="p">)</span>

    <span class="k">def</span> <span class="nf">_delete_primary_key_sql</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">model</span><span class="p">,</span> <span class="n">name</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_delete_constraint_sql</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">sql_delete_pk</span><span class="p">,</span> <span class="n">model</span><span class="p">,</span> <span class="n">name</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">remove_procedure</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">procedure_name</span><span class="p">,</span> <span class="n">param_types</span><span class="o">=</span><span class="p">()):</span>
        <span class="n">sql</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">sql_delete_procedure</span> <span class="o">%</span> <span class="p">{</span>
            <span class="s1">&#39;procedure&#39;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">quote_name</span><span class="p">(</span><span class="n">procedure_name</span><span class="p">),</span>
            <span class="s1">&#39;param_types&#39;</span><span class="p">:</span> <span class="s1">&#39;,&#39;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">param_types</span><span class="p">),</span>
        <span class="p">}</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="n">sql</span><span class="p">)</span></div>
</pre></div>

          </div>
        </div>
      </div>
      
        
          <div class="yui-b" id="sidebar">
            
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
<div id="searchbox" style="display: none" role="search">
  <h3>Quick search</h3>
    <div class="searchformwrapper">
    <form class="search" action="../../../../../search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    </div>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
              <h3>Last update:</h3>
              <p class="topless">Mar 04, 2020</p>
          </div>
        
      
    </div>

    <div id="ft">
      <div class="nav">
    <a href="../../../../index.html" title="Module code" accesskey="U">up</a></div>
    </div>
  </div>

      <div class="clearer"></div>
    </div>
  </body>
</html>